#include "stdafx.h"
#include "mainwindow.h"
#include <float.h>

#define ztrcpy	_tcscpy
#define ztrcmp	_tcscmp
#define ztrlen	_tcslen
#define ztrcat	_tcscat
#define zscanf	_stscanf
#define zprintf	_stprintf

Translhextion mainwindow;
HINSTANCE hMainInstance;
HWND hDlgCurrent=NULL;

// returns 8 bits
int literal_u8(u8 *str)
{
	return *str;
}

// returns 16 bits
int literal_u16(u8 *str)
{
	return *((u16*)str);
}

// returns low-case char, 8 bits
int charcase_u8(u8 *str)
{
	u8 c=*str;

	if (c >= 'A' && c <= 'Z') return 'a' + c - 'A';
	else return c;
}

// returns low-case char, wide
int charcase_wide(u8 *str)
{
	wchar_t c=*((wchar_t*)str);

	if (c >= 'A' && c <= 'Z') return 'a' + c - 'A';
	else return c;
}

template<class T>void sswap(T& x, T& y)
{
	T temp = x;
	x = y;
	y = temp;
}

char aa=0;

void strHex2Char()
{
	TCHAR* pcTemp;
	aa=0;
	int i,q=_tcslen(pcFWText);
	if(q%2)
	{
		
		for(i=0;i<q;i++)pcFWText[q+i]=pcFWText[i];
		pcFWText[q*2]=0;aa=1;
	}
	for(i=q=0;pcFWText[i]!='\0';i+=2)
	{
		pcTemp=&pcFWText[i];

		_stscanf(pcTemp,_T("%2x"),&cBuf[q]);

		q++;
	}
	buflen=q;
	cBuf[q]='\0';
}

static TCHAR *windowText=NULL;

GString GetWindowText(HWND &hDlg, int nIDDlgItem)
{
	// retreive item size
	int size = SendMessage (GetDlgItem (hDlg, nIDDlgItem), EM_GETLIMITTEXT, 0, 0);
	if(!windowText) windowText = new TCHAR[size];
	GetDlgItemText (hDlg, nIDDlgItem, windowText, size);

	return windowText;
}

CLanguageHelper::CLanguageHelper()
{
	//hModule=GetModuleHandle(NULL);
	SetLanguage(LANG_DEF);
}

void CLanguageHelper::SetLanguage(WORD wLang)
{
	lang=wLang;
}

GString CLanguageHelper::LoadString(UINT uID)
{
	GString dest;

	HRSRC hRes=FindResourceEx(hMainInstance/*hModule*/,RT_STRING,MAKEINTRESOURCE(uID/ 16 + 1),lang);
	if(hRes)
	{
		HGLOBAL hGlobal=LoadResource(hMainInstance/*hModule*/,hRes);
		if(hGlobal)
		{
			LPCTSTR lpStringTable=(LPCTSTR)LockResource(hGlobal);
			if(lpStringTable)
			{
				LPCTSTR pt=lpStringTable;
				// okay now walk the string table
				for (int i = 0; i < (int)( uID & 15); i++)
					pt += 1 + (UINT)*pt;
				dest.Copy(pt,*pt,1);
				UnlockResource(lpStringTable);
			}
			FreeResource(hGlobal);
		}
	}

	return dest;
}

CLanguageHelper lang_helper;

void ReleaseWindowText()
{
	if(windowText)
	{
		delete[] windowText;
		windowText=NULL;
	}
}

GString GetClipboardText()
{
	GString cBuf;

	if (OpenClipboard (GetForegroundWindow()))
	{
		HGLOBAL hClipMemory;
#ifndef UNICODE
		hClipMemory=GetClipboardData (CF_TEXT);
#else
		hClipMemory = GetClipboardData (CF_UNICODETEXT);
#endif
		if(hClipMemory)
		{
			if (GlobalSize (hClipMemory) > 0)
			{
#ifndef UNICODE
				cBuf.Set(GlobalLock (hClipMemory),GString::type_ascii);
#else
				cBuf=(TCHAR*) GlobalLock (hClipMemory);
#endif
			}
			GlobalUnlock (hClipMemory);
		}
		CloseClipboard ();
	}

	return cBuf;
}

void TextToClipboard( TCHAR* pcText )
{
	
	int len = 1 + _tcslen( pcText );
	HGLOBAL hGlobal = GlobalAlloc( GHND, len );
	if( hGlobal != NULL )
	{
		SetCursor (LoadCursor (NULL, IDC_WAIT));
		TCHAR* pd = (TCHAR*) GlobalLock (hGlobal);
		if( pd )
		{
			
			_tcscpy( pd, pcText );
			GlobalUnlock (hGlobal);
			if(OpenClipboard(NULL))
			{
				
				EmptyClipboard();
				SetClipboardData (CF_TEXT, hGlobal);
				CloseClipboard (); 
			}
			else 
				MessageBox (NULL,LoadString(IDS_COPY_NO_ACCESS),LoadString(IDS_TITLE_COPY),MB_OK | MB_ICONERROR);
		}
		else
		{
			GlobalFree(hGlobal);
			MessageBox (NULL,LoadString(IDS_COPY_CANT_LOCK),LoadString(IDS_TITLE_COPY),MB_OK | MB_ICONERROR);
		}
		SetCursor (LoadCursor (NULL, IDC_ARROW));
	}
	else
		MessageBox (NULL,LoadString(IDS_COPY_CANT_ALLOCATE),LoadString(IDS_TITLE_COPY),MB_OK | MB_ICONERROR);
}

VOID WINAPI AddBitmapMenu(HWND hwnd, // window that owned the menu bar
	LPTSTR lpszText, // text of menu name on menu bar
	UINT uID, // ID of first bitmap menu item
	HBITMAP *paHbm, // bitmaps for the menu items
	int cItems) // number bitmap menu items
{
	HMENU hmenuBar = GetMenu(hwnd);
	HMENU hmenuPopup = CreatePopupMenu();
	MENUITEMINFO mii;

	int i;
	// Add the bitmap menu items to the menu.

	for (i = 0; i < cItems; i++)
	{
		mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_DATA;
		mii.fType = MFT_BITMAP;
		mii.wID = uID + i;
		mii.dwTypeData = (LPTSTR) (paHbm[i]);
		InsertMenuItem(hmenuPopup, i, TRUE, &mii);
	}

	// Add a menu name to the menu bar.
	mii.fMask = MIIM_TYPE | MIIM_DATA | MIIM_SUBMENU;
	mii.fType = MFT_STRING;
	mii.hSubMenu = hmenuPopup;
	mii.dwTypeData = lpszText;
	InsertMenuItem(hmenuBar, GetMenuItemCount(hmenuBar), TRUE, &mii);
}

#include "dialog.h"

int WINAPI wWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, TCHAR* szCmdLine, int iCmdShow)
{
	hMainInstance = hInstance;

	HACCEL hAccel;

	SetAppInstance(hInstance);
	MSG msg;

	mainwindow.Create();
	ShowWindow(mainwindow, iCmdShow);
	UpdateWindow(mainwindow);
	mainwindow.SetFocus();

	//if( szCmdLine != NULL && _tcslen( szCmdLine ) != 0 )
	//{
	//	TCHAR sz[MAX_PATH];
	//	TCHAR* p = szCmdLine;
	//	_tcsncpy( sz, szCmdLine, MAX_PATH );
	//	for( int i = 0; i < MAX_PATH; ++i )
	//	{
	//		TCHAR c = sz[i];
	//		if( c == 0 )
	//		{
	//			*p = 0;
	//			break;
	//		}
	//		else if( c != '"' )
	//		{
	//			*p++ = c;
	//		}
	//	}
	//	
	//	TCHAR lpszPath[MAX_PATH];
	//	HRESULT hres = ResolveIt(mainwindow, szCmdLine, lpszPath );
	//	if( SUCCEEDED( hres ) )
	//	{
	//		int ret = MessageBox(mainwindow, LoadString(IDS_NOTICE_LINK), LoadString(IDS_SYSTEM_NAME), MB_YESNOCANCEL | MB_ICONQUESTION );
	//		switch( ret )
	//		{
	//		case IDYES:
	//			mainwindow.load_file( lpszPath );
	//			break;
	//		case IDNO:
	//			mainwindow.load_file( szCmdLine );
	//			break;
	//		case IDCANCEL:
	//			break;
	//		}
	//	}
	//	else
	//	{
	//		mainwindow.load_file( szCmdLine );
	//	}
	//}

	hAccel = LoadAccelerators (hInstance, MAKEINTRESOURCE (IDR_ACCELERATOR1));

	while (GetMessage (&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator (mainwindow, hAccel, &msg) || NULL == hDlgCurrent || !IsDialogMessage(hDlgCurrent, &msg)
			/*!TranslateAccelerator (mainwindow, hAccel, &msg)*/)
		{
			TranslateMessage (&msg);
			DispatchMessage (&msg);
		}
	}
	return msg.wParam;
}

static Translhextion *pWindow;
static BUTTON_REF butref[]=
{
	16,16,
	IDM_NEW,0,
	IDM_OPEN,1,
	IDM_SAVE,2,
	IDM_SAVE_AS,21,
	IDM_EDIT_READONLYMODE,18,
	IDM_PROPERTIES,23,
	IDM_OPEN_TEXT,24,
	IDM_EXIT,16,
	IDM_EDIT_CUT,6,
	IDM_EDIT_COPY,7,
	IDM_EDIT_PASTE,8,
	IDM_FIND,3,
	IDM_HEXFIND,4,
	IDM_FINDNEXT,11,
	IDM_FINDPREV,12,
	IDM_REPLACE,10,
	IDM_OPEN_TABLE,20,
	IDM_TABLE_CONVERT,22,
	IDM_SCRIPT_DUMP,14,
	IDM_REPLACE_SCRIPT,17,
	IDM_VIEW_SETTINGS,5,
	IDM_COLOR_SETTINGS,19,
	IDM_OPTIMIZESCRIPT,13,
	IDM_ABOUT,15
};

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	CREATESTRUCT* pCS;

	switch (message)
	{
	case WM_CREATE:
		pCS = (CREATESTRUCT*) lParam;
		pWindow = (Translhextion*) pCS->lpCreateParams;
		pWindow->SetWindowPtr(hWnd,pWindow);

		pWindow->on_window_init(hWnd, hMainInstance );
		if (!pWindow->m_Menu.OnCreate(hWnd)) return -1;
		pWindow->m_Menu.LoadBitmap(MAKEINTRESOURCE(IDB_MAINTOOLBAR32));
		pWindow->m_Menu.SetButtonRef(butref,sizeof(butref)/sizeof(*butref));

		pWindow->m_Frame.SetFocus();
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	
	if(pWindow) return pWindow->OnMessage(message,wParam,lParam);
	return DefWindowProc(hWnd, message, wParam, lParam);
}

BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam )
{
	TCHAR cBuf[64];
	GString match;
	
	match.Format(_T("%s wndclass"),sName);

	if( GetClassName( hwnd, cBuf, 64 ) != 0 )
	{
		if( match.Compare(cBuf) == 0 )
		{
			iNumInstances++;
		}
	}
	return TRUE;
}

int iNumInstances;

void count_instances()
{
	iNumInstances = 0;
	EnumWindows( (WNDENUMPROC) EnumWindowsProc, 0 );
}

BOOL CALLBACK IMEBarDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			if(bInsertMode)
				SetWindowText (hDialog, LoadString(IDS_IME_INSERT));
			else
				SetWindowText (hDialog, LoadString(IDS_IME_OVERWRITE));
			if(IMEPos.left == 0 && IMEPos.top == 0)
				SetWindowPos(hDialog,HWND_TOP,mainwindow.iWindowX,mainwindow.iWindowY+mainwindow.iWindowHeight-100,0,0,SWP_NOSIZE);// | SWP_NOZORDER);
			else
				SetWindowPos(hDialog,HWND_TOP,IMEPos.left,IMEPos.top,0,0,SWP_NOSIZE);			
			SetFocus (GetDlgItem (hDialog, IDC_EDIT1)); 
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDC_EDIT1:
			{
				bDoingIME = true;
				TCHAR cBuf[500];
				GetDlgItemText (hDialog, IDC_EDIT1, cBuf, 500);
				_tstring a = (_tstring) cBuf;
				_tstring b;
				b = L"";
				long numbytes = 0;
				//if(a.length() > 0)
				std::vector<_tstring> phrase;
				int c = 0;
				for(c = 0;c<(int)a.length();c++)
				{
					if(a.substr(c,1) == L"\\")
					{
						b += sByteOpen + L"END" + sByteClose + L"\n";
						phrase.push_back(b);
						phrase.push_back(L"\n");
						b = L"";
					}
					else if(a.substr(c,1) == L"*")
					{
						b += L"\n";
						phrase.push_back(b);
						b = L"";
					}
					else
					{ 
						b += a.substr(c,1);
					}
				}
				phrase.push_back(b);
				bool oldbmark = bByteMark;
				bByteMark = false;
				mainwindow.table.descript(phrase);
				bByteMark = oldbmark;
				mainwindow.table.detranslate(phrase);
				_tstring output2 = L"";
				for(c = 0;c < (int)phrase.size();c++)
				{
					output2 += phrase[c];
				}
				vIMEBytes = phrase;
				numbytes = phrase.size();
				bByteMark = true;
				bSplitMultiByte = false;
				mainwindow.table.translate(phrase);
				bSplitMultiByte = true;
				bByteMark = oldbmark;
				_tstring output = L"";
				int i = 0;
				for(i = 0;i<(int)phrase.size();i++)
				{
					if(phrase[i] == L"\n")
					{
						output += L"*";
					}
					else if(phrase[i].length() >= 5 && phrase[i].substr(1,3) == L"END")
					{
						output += sByteOpen + L"END" + sByteClose;
					}
					else
					{
						output += phrase[i];
					}
				}
				if(output.length() > 50)
				{
					output = output.substr(output.length()-50,50);
				}
				if(output2.length() > 50)
				{
					output2 = output2.substr(output2.length()-50,50);
				}
				SetDlgItemText(hDialog,IDC_INPUT,output.c_str());
				SetDlgItemText(hDialog,IDC_BYTES,(stringvalue(numbytes) + L" bytes").c_str());
				SetDlgItemText(hDialog,IDC_HEX,output2.c_str());   
				
				bDoingIME = false;
				if(numbytes >= 100)
				{
					GetWindowRect(hDialog,&IMEPos);
					EndDialog(hDialog,0);
				}
				return TRUE;
			}
		case IDOK:
			{
				bIMEDone = true;
				GetWindowRect(hDialog,&IMEPos);
				EndDialog(hDialog,0);
				return TRUE;
			}
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK ToolBarDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			//if(bTableActive)
			//{
			//	CheckDlgButton( hDialog, IDC_CHECK1, BST_CHECKED );
			//}
			if(bThingyView)
			{
				CheckDlgButton( hDialog, IDC_CHECK2, BST_CHECKED );
			}
			if(bJapanese)
			{
				if(bJPShift)
				{
					CheckDlgButton( hDialog, IDC_RADIO3, BST_CHECKED );
				}
				else
				{
					CheckDlgButton( hDialog, IDC_RADIO4, BST_CHECKED );
				}
			}
			if(!bJPuncSwitch)
			{
				CheckDlgButton( hDialog, IDC_CHECK4, BST_CHECKED );
			}
			if(bDumpClean)
			{
				CheckDlgButton( hDialog, IDC_CHECK3, BST_CHECKED );
			}
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		//case IDC_CHECK1:
		//	{
		//		if(IsDlgButtonChecked(hDialog,IDC_CHECK1))
		//		{
		///			bTableActive = true;
		//		}
		//		else
		//		{
		//			bTableActive = false;
		//		}
		///		SetFocus(GetParent(hDialog));
		//		return TRUE;
		//	}

		case IDC_CHECK2:
			{
			if(IsDlgButtonChecked(hDialog,IDC_CHECK2))
				{
					bThingyView = true;
					if(bAutomaticBPL)
					{
						bAutomaticBPL = false;
						iOldBPL = iBytesPerLine;
						iBytesPerLine = 16;
						bOldAutomaticBPL = true;
					}
					mainwindow.repaint();
					mainwindow.update_size_change();
				}
				else
				{
					bThingyView = false;
					if(bOldAutomaticBPL)
					{
						bAutomaticBPL = true;
						iBytesPerLine = iOldBPL;
						bOldAutomaticBPL = false;
					}
					mainwindow.repaint();
					mainwindow.update_size_change();
				}
				SetFocus(GetParent(hDialog));
			return TRUE;
			}
		case IDC_CHECK6:
			{
				if(IsDlgButtonChecked(hDialog,IDC_CHECK6))
				{
					bJapanese = true;
					bJPShift = true;
					bJPEUC = false;
					CheckDlgButton(hDialog, IDC_RADIO3, BST_CHECKED);
					mainwindow.repaint(); 
					mainwindow.update_size_change();
				}
				else
				{
					bJapanese = false;
					bJPShift = false;
					bJPEUC = false;
					CheckDlgButton(hDialog, IDC_RADIO3, BST_UNCHECKED);
					CheckDlgButton(hDialog, IDC_RADIO4, BST_UNCHECKED);
					mainwindow.repaint(); 
					mainwindow.update_size_change();
				}
				SetFocus(GetParent(hDialog));
			return TRUE;
			}	
			case IDC_RADIO3:
			{
				if(IsDlgButtonChecked(hDialog,IDC_RADIO3))
				{
					bJapanese = true;
					CheckDlgButton(hDialog,IDC_CHECK6,BST_CHECKED);
					bJPShift = true;
					bJPEUC = false;
					mainwindow.repaint(); 
					mainwindow.update_size_change();
				}
				SetFocus(GetParent(hDialog));
			return TRUE;
			}
			case IDC_RADIO4:
			{
				if(IsDlgButtonChecked(hDialog,IDC_RADIO4))
				{
					bJapanese = true;
					CheckDlgButton(hDialog,IDC_CHECK6,BST_CHECKED);
					bJPEUC = true;
					bJPShift = false;
					mainwindow.repaint(); 
					mainwindow.update_size_change();
				}
				SetFocus(GetParent(hDialog));
			return TRUE;
			}
		case IDC_CHECK3:
			{
				if(IsDlgButtonChecked(hDialog,IDC_CHECK3))
				{
					bDumpClean = true;
					mainwindow.table.vTableFileLeft.push_back(_T("#"));
					mainwindow.table.vTableFileRight.push_back(_T(""));
					fstream fs;
					fs.open(sLoadedTable.c_str(),ios::out);
					save_table_file(fs);
					fs.close();
				}
				else
				{
					bDumpClean = false;
					int k = 0;
					for(k = 0;k<(int)mainwindow.table.vTableFileLeft.size();k++)
					{
						if(mainwindow.table.vTableFileLeft[k] == _T("#"))
						{
							//vTableFileLeft.remove(k);
							mainwindow.table.vTableFileLeft.erase(mainwindow.table.vTableFileLeft.begin()+k);
							//vTableFileRight.remove(k);
							mainwindow.table.vTableFileRight.erase(mainwindow.table.vTableFileLeft.begin()+k);
							break;
						}
					}
					fstream fs;
					fs.open(sLoadedTable.c_str(),ios::out);
					save_table_file(fs);
					fs.close();
				}
				
				SetFocus(GetParent(hDialog));
				return TRUE;
			}
		case IDC_CHECK4:
			{
				if(IsDlgButtonChecked(hDialog,IDC_CHECK4))
				{
					bJPuncSwitch = false;
					mainwindow.table.vTableFileLeft.push_back(_T(">"));
					mainwindow.table.vTableFileRight.push_back(_T(""));
					fstream fs;
					fs.open(sLoadedTable.c_str(),ios::out);
					save_table_file(fs);
					fs.close();
				}
				else
				{
					bJPuncSwitch = true;
					int k = 0;
					for(k = 0;k<(int)mainwindow.table.vTableFileLeft.size();k++)
					{
						if(mainwindow.table.vTableFileLeft[k] == _T(">"))
						{
							//vTableFileLeft.remove(k);
							mainwindow.table.vTableFileLeft.erase(mainwindow.table.vTableFileLeft.begin()+k);
							//vTableFileRight.remove(k);
							mainwindow.table.vTableFileRight.erase(mainwindow.table.vTableFileRight.begin()+k);
							break;
						}
					}
					fstream fs;
					fs.open(sLoadedTable.c_str(),ios::out);
					save_table_file(fs);
					fs.close();
				}
				
				SetFocus(GetParent(hDialog));
				return TRUE;
			}
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK BinaryModeDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			if (iBinaryModeSetting == LITTLE_ENDIAN)
				CheckDlgButton (hDialog, IDC_RADIO1, BST_CHECKED);
			else
				CheckDlgButton (hDialog, IDC_RADIO2, BST_CHECKED);
			SetFocus(hDialog);
			
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				if (IsDlgButtonChecked (hDialog, IDC_RADIO1) == BST_CHECKED)
					iBinaryModeSetting = LITTLE_ENDIAN;
				else
					iBinaryModeSetting = BIG_ENDIAN;
				EndDialog (hDialog, 1);
				return TRUE;
			}
	
		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK OffsetModeDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			//if (iOffsetMode == STANDARDHEX)
			//	CheckDlgButton (hDialog, IDC_RADIO1, BST_CHECKED);
			//else if(iOffsetMode == LOROM)
			//	CheckDlgButton (hDialog, IDC_RADIO2, BST_CHECKED);
			//else
				CheckDlgButton (hDialog, IDC_RADIO3, BST_CHECKED);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT1), stringvalue(SNESHEADER).c_str());
			SetFocus(hDialog);
			
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				//if (IsDlgButtonChecked (hDialog, IDC_RADIO1) == BST_CHECKED)
				//	iOffsetMode = STANDARDHEX;
				//else if(IsDlgButtonChecked (hDialog, IDC_RADIO2) == BST_CHECKED)
				//	iOffsetMode = LOROM;
				//else
					iOffsetMode = HIROM;
				int len = SendMessage (GetDlgItem (hDialog, IDC_EDIT1), EM_GETLIMITTEXT, 0, 0);
				TCHAR *num = new TCHAR[len];
				int result = GetDlgItemText (hDialog, IDC_EDIT1, num, len);
				_tstring x = (_tstring) num;
				delete [] num;
				num = NULL;
				SNESHEADER = intvalue(x);
				EndDialog (hDialog, 1);
				return TRUE;
			}
	
		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK ScriptReplaceDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			GString cBuf;
			//if(iOffsetMode == LOROM)
			//	cBuf.Format(_T("x%X"), toLOROM(iCopyStartOffset));
			//else if(iOffsetMode == HIROM)
			//	cBuf.Format(_T("x%X"), toHIROM(iCopyStartOffset));
			//else
				cBuf.Format(_T("x%X"), iCopyStartOffset);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT1), cBuf);
			//if(iOffsetMode == LOROM)
			//	cBuf.Format(_T("x%X"), toLOROM(iCopyEndOffset));
			//else if(iOffsetMode == HIROM)
			//	cBuf.Format(_T("x%X"), toHIROM(iCopyEndOffset));
			//else
				cBuf.Format(_T("x%X"), iCopyEndOffset);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT2), cBuf);
			CheckDlgButton (hDialog, IDC_RADIO1, BST_CHECKED);
			if(bDefByteMark)
				CheckDlgButton(hDialog,IDC_CHECK6, BST_CHECKED);
			SetFocus(hDialog);
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				TCHAR cBuf[64];
				// Read start offset.
				if(IsDlgButtonChecked(hDialog,IDC_CHECK6))
				{
					bByteMark = true;
				}
				else
				{
					bByteMark = false;
				}
				if(IsDlgButtonChecked(hDialog,IDC_CHECK5))
				{
					bPadBytes = true;
				}
				else
				{
					bPadBytes = false;
				}
				if(IsDlgButtonChecked(hDialog,IDC_RADIO1))
				{
					bReplaceToOffset = false;
				}
				else
				{
					bReplaceToOffset = true;
				}

				if (GetDlgItemText (hDialog, IDC_EDIT1, cBuf, 64) != 0)
				{
					int i;
					if (_stscanf (cBuf, _T("x%X"), &i) == 0)
					{
						if (_stscanf (cBuf, _T("%d"), &i) == 0)
						{
							MessageBox (hDialog, LoadString(IDS_SREP_ERR_UNK_ADDRESS), LoadString(IDS_TITLE_SCRIPT_REP_ERROR), MB_OK | MB_ICONERROR);
							i = -1;
						}
					}
					if (i==-1)
					{
						EndDialog (hDialog, 0);
						return 0;
					}
					//if(iOffsetMode == LOROM)
					//	iCopyStartOffset = fromLOROM(i);
					//else if(iOffsetMode == HIROM)
					//	iCopyStartOffset = fromHIROM(i);
					//else
						iCopyStartOffset = i;
				}

				CFileDialog dlg;
				dlg.Create(hDialog,LoadString(IDS_FILTER_SCRIPT),_T(""),OFN_HIDEREADONLY | OFN_FILEMUSTEXIST);

				if (dlg.DoModal())
				{
					fstream fs;
					sFileName=FixFilename(dlg.GetFileName(),_T(".txt"));
					/* AND...? */
					EndDialog (hDialog, 1);
					return TRUE;
				}
				else
				{
					//EndDialog (hDialog, 0);
				}
				break;
			}

		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK ProgressDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			SetFocus(hDialog);
			return FALSE;
		}

	case WM_COMMAND:
		//switch (LOWORD (wParam))
		{
			//default:
			return FALSE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK InfoDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
		case WM_INITDIALOG:
		{
			long filesize = ppair.x;
			long filesize2 = ppair.y;
			progress1 = GetDlgItem(hDialog,IDC_PROGRESS1);
			progress2 = GetDlgItem(hDialog,IDC_PROGRESS2);
			SetProgressColor(progress1,RGB(170,55,255));
			SetProgressColor(progress2,RGB(170,55,255));
			SetProgressRange(progress1,0,filesize);
			SetProgressRange(progress2,0,filesize);
			SetProgressPos(progress1,filesize + 1);
			SetProgressPos(progress2,filesize2);
			//_tstring cresults;
			long percent = 100;
			if(filesize != 0)
			{
				percent = (long) ((100.0 / filesize) * filesize2);
			}
			GString cresults;
			cresults.Format(LoadString(IDS_COMPRESSION_RESULT),filesize,filesize2,percent);
			SetWindowText(GetDlgItem(hDialog,IDC_CRESULTS),cresults);
			
			SetFocus(hDialog);
			return FALSE;
		}

		case WM_COMMAND:
		{
			switch (LOWORD (wParam))
			{
				case IDOK:
				{
					EndDialog(hDialog,0);
					return FALSE;
					
				}
				break;
			}
		}
	}
	return FALSE;
}

BOOL CALLBACK ScriptDumpDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			GString cBuf;
			//if(iOffsetMode == LOROM)
			//	cBuf.Format(_T("x%X"), toLOROM(iCopyStartOffset));
			//else if(iOffsetMode == HIROM)
			//	cBuf.Format(_T("x%X"), toHIROM(iCopyStartOffset));
			//else
				cBuf.Format(_T("x%X"), iCopyStartOffset);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT1), cBuf);
			//if(iOffsetMode == LOROM)
			//	cBuf.Format(_T("x%X"), toLOROM(iCopyEndOffset));
			//else if(iOffsetMode == HIROM)
			//	cBuf.Format(_T("x%X"), toHIROM(iCopyEndOffset));
			//else
				cBuf.Format(_T("x%X"), iCopyEndOffset);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT2), cBuf);
			cBuf.Format(_T("%d"), iCopyEndOffset-iCopyStartOffset+1);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT3), cBuf);
			CheckDlgButton (hDialog, IDC_RADIO1, BST_CHECKED);
			if(iLastDumpTarget == 0)
				CheckDlgButton(hDialog,IDC_RADIO3,BST_CHECKED);
			else
				CheckDlgButton(hDialog,IDC_RADIO4,BST_CHECKED);
			if(bDefByteMark)
				CheckDlgButton(hDialog,IDC_CHECK1,BST_CHECKED);
			SetFocus(hDialog);
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				TCHAR cBuf[64];
				if(IsDlgButtonChecked(hDialog,IDC_CHECK1))
				{
					bByteMark = true;
				}
				else
				{
					bByteMark = false;
				}
				if(IsDlgButtonChecked(hDialog,IDC_RADIO3))
				{
					bDumpToFile = true;
					iLastDumpTarget = 0;
				}
				else
				{
					bDumpToFile = false;
					iLastDumpTarget = 1;
				}
				if (GetDlgItemText (hDialog, IDC_EDIT1, cBuf, 64) != 0)
				{
					int i;
					if (_stscanf (cBuf, _T("x%X"), &i) == 0)
					{
						if (_stscanf (cBuf, _T("%d"), &i) == 0)
						{
							MessageBox (hDialog, _T("Starting offset is not recognized."), _T("Script dump error:"), MB_OK | MB_ICONERROR);
							i = -1;
						}
					}
					if (i==-1)
					{
						EndDialog (hDialog, 0);
						return 0;
					}
					//if(iOffsetMode == LOROM)
					//	iCopyStartOffset = fromLOROM(i);
					//else if(iOffsetMode == HIROM)
					//	iCopyStartOffset = fromHIROM(i);
					//else
						iCopyStartOffset = i;
				}
				switch (IsDlgButtonChecked (hDialog, IDC_RADIO1))
				{
				case BST_CHECKED: 
					{
						if (GetDlgItemText (hDialog, IDC_EDIT2, cBuf, 64) != 0)
						{
							int i;
							if (_stscanf (cBuf, _T("x%X"), &i) == 0)
							{
								if (_stscanf (cBuf, _T("%d"), &i) == 0)
								{
									MessageBox (hDialog, _T("Ending offset is not recognized."), _T("Script dump error:"), MB_OK | MB_ICONERROR);
									i = -1;
								}
							}
							if (i==-1)
							{
								EndDialog (hDialog, 0);
								return 0;
							}
							//if(iOffsetMode == LOROM)
							//	iCopyEndOffset = fromLOROM(i);
							//else if(iOffsetMode == HIROM)
							//	iCopyEndOffset = fromHIROM(i);
							//else
								iCopyEndOffset = i;
						}
						break;
					}

				default: 
					{
						if (GetDlgItemText (hDialog, IDC_EDIT3, cBuf, 64) != 0)
						{
							int i;
							if (_stscanf (cBuf, _T("%d"), &i) == 0)
							{
								MessageBox (hDialog, _T("Number of bytes is not recognized."), _T("Script dump error:"), MB_OK | MB_ICONERROR);
								i = -1;
							}
							if (i==-1)
							{
								EndDialog (hDialog, 0);
								return 0;
							}
							iCopyEndOffset = iCopyStartOffset + i;
						}
						break;
					}
				}
				if(bDumpToFile)
				{
					CFileDialog dlg;
					dlg.Create(hDialog,LoadString(IDS_FILTER_SCRIPT),LoadString(IDS_FILTER_SCRIPT_TITLE),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,false);

					if (dlg.DoModal())
					{
						//fstream fs;
						//int cur = _tcslen(cFileName) - 4;
						//if(uppercase(((std::string) cFileName).substr(((string) cFileName).length()-4,4)) == ".TXT")
						//{
						//	sFileName = (string) cFileName;
						//}
						//else
						//{
						//	sFileName = (string) ztrcat(cFileName,".txt");
						//}
						sFileName=FixFilename(dlg.GetFileName(),_T(".txt"));
						EndDialog (hDialog, 1);
						return TRUE;
					}
					else
						//EndDialog (hDialog, 0);
					break;
				}
				else
				{
				
					EndDialog (hDialog, 1);
					return TRUE;
				}
				//return TRUE;
			}

		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK ViewScriptDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			SetCursor (LoadCursor (NULL, IDC_WAIT));
			int i = 0;
			assert(vHexdoc.size() >= 0);
			for(i = 0;i<(int)vHexdoc.size();i++)
			{
				if(vHexdoc[i].substr(vHexdoc[i].length()-1,1) == L"\n")
					sScriptDump += vHexdoc[i].substr(0,vHexdoc[i].length()-1) + L"\r\n";
				else
					sScriptDump += vHexdoc[i].substr(0,vHexdoc[i].length());
				
			}
			SetCursor (LoadCursor (NULL, IDC_ARROW));
			SetWindowText( GetDlgItem ( hDialog, IDC_SCRIPTEDIT), sScriptDump.c_str());
			SetWindowText( GetDlgItem ( hDialog, IDC_NUMBYTES), (_T("Total Bytes: ") + stringvalue(iDumpEnd+1 - iDumpStart)).c_str());
			SetWindowText( GetDlgItem ( hDialog, IDC_STARTBYTE), stringvalue(iDumpStart).c_str());
			SetWindowText( GetDlgItem ( hDialog, IDC_ENDBYTE), stringvalue(iDumpEnd).c_str());
			if(!bByteMark)
			{
				CheckDlgButton(hDialog,IDC_CHECK1,BST_CHECKED);
			}
			SetFocus(hDialog);
			dumps++;
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				sScriptDump = L"";
				SetWindowText( GetDlgItem ( hDialog, IDC_SCRIPTEDIT), sScriptDump.c_str());
				EndDialog(hDialog,0);
				return FALSE;
			}

		case IDC_REINSERT:
			{
				if(IsDlgButtonChecked(hDialog,IDC_CHECK1))
				{
					bByteMark = false;
				}
				else
				{	
					bByteMark = true;
				}	
				SetCursor (LoadCursor (NULL, IDC_WAIT));
				int i = 0;
				int j = 0;
				int numlines = SendMessage(GetDlgItem (hDialog, IDC_SCRIPTEDIT),EM_GETLINECOUNT,0,0);
				int linelength = 0;
				for(i = 0;i<numlines;i++)
				{
					linelength += 2 + SendMessage(GetDlgItem (hDialog, IDC_SCRIPTEDIT),EM_LINELENGTH,SendMessage(GetDlgItem (hDialog, IDC_SCRIPTEDIT),EM_LINEINDEX,i,0),0);
				}
				linelength = linelength - 1;
				TCHAR* cBuf = new TCHAR[ linelength ];
					GetDlgItemText( hDialog, IDC_SCRIPTEDIT, cBuf, linelength );
				//else
				//{
				///	MessageBox( hDialog, "Could not store reinsertion data.", "Reinsertion Failed!", MB_OK | MB_ICONERROR );
				//	break;
				//}
				int	scriptsize = sScriptDump.length();
				sScriptDump = L"";
				sScriptDump = (_tstring) cBuf;
				_tstring temp;
				vHexdoc = vEmpty;
				for(i = 0;i<(int)sScriptDump.length();i++)
				{
					if(sScriptDump.substr(i,6) == sByteOpen + L"END" + sByteClose + L"\r")
					{
						vHexdoc.push_back(temp + sByteOpen + L"END" + sByteClose + L"\n");
						temp = L"";
						i = i + 6;
					}
					else if(sScriptDump.substr(i,1) == L"\r")
					{
						vHexdoc.push_back(temp + L"\n");
						temp = L"";
						i++;
					}
					else
					{
						temp += sScriptDump.substr(i,1);
					}
				}
				if(temp != L"")
					vHexdoc.push_back(temp);
				delete [] cBuf;
				sScriptDump = L"";
				SetWindowText( GetDlgItem ( hDialog, IDC_SCRIPTEDIT), sScriptDump.c_str());
				SetCursor (LoadCursor (NULL, IDC_ARROW));
				EndDialog(hDialog,1);
				return TRUE;
			}
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK AddBookmarkDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	int i = -1;
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
		GString cBuf;
		cBuf.Format(_T("x%X"), iBookmarkOffset);
		SetWindowText (GetDlgItem (hDialog, IDC_EDIT1), cBuf);
		SetFocus(hDialog);
		return FALSE;
		}
	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			TCHAR cBuf[16];
			if (GetDlgItemText (hDialog, IDC_EDIT1, cBuf, 16) != 0)
			{
				if (_stscanf (cBuf, _T("x%X"), &i) == 0)
				{
					if (_stscanf (cBuf, _T("%d"), &i) == 0)
					{
						MessageBox (hDialog, _T("Starting offset is not recognized."), _T("Add bookmark error:"), MB_OK | MB_ICONERROR);
						i = -1;
					}
				}
				if (i==-1)
				{
					EndDialog (hDialog, 0);
					return 0;
				}
				iBookmarkOffset = i;
			}
			GetDlgItemText (hDialog, IDC_EDIT2, pcBookmarkTxt, iBookmarkMaxChars);
			EndDialog (hDialog, 1);
			return TRUE;

		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}


BOOL CALLBACK RemoveBookmarkDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			GString cBuf;
			int i;
			HWND hwndList = GetDlgItem (hDialog, IDC_LIST1);
			for (i=0; i<iRemBookmark; i++)
			{
				if (pbmkRemove[i].name == NULL)
				{
					//if(iOffsetMode == LOROM)
					//	cBuf.Format(_T("%d) 0x%X"), i+1, toLOROM(pbmkRemove[i].offset));
					//else if(iOffsetMode == HIROM)
					//	cBuf.Format(_T("%d) 0x%X"), i+1, toHIROM(pbmkRemove[i].offset));
					//else
						cBuf.Format(_T("%d) 0x%X"), i+1, pbmkRemove[i].offset);
				}
				else
				{
					//if(iOffsetMode == LOROM)
					//	cBuf.Format(_T("%d) 0x%X:%s"), i+1, toLOROM(pbmkRemove[i].offset), pbmkRemove[i].name);
					//else if(iOffsetMode == HIROM)
					//	cBuf.Format(_T("%d) 0x%X:%s"), i+1, toHIROM(pbmkRemove[i].offset), pbmkRemove[i].name);
					//else
						cBuf.Format(_T("%d) 0x%X:%s"), i+1, pbmkRemove[i].offset, pbmkRemove[i].name);
				}
				SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR)cBuf);
			}
			SendMessage (hwndList, LB_SETCURSEL, 0, 0);
			SetFocus(hDialog);
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				HWND hwndList = GetDlgItem (hDialog, IDC_LIST1);
				iRemBookmark = SendMessage (hwndList, LB_GETCURSEL, 0, 0);
				EndDialog (hDialog, 1);
				return TRUE;
			}
	
		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK TableConvertDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			int i = 0;
			for(i = 0;i<(int)mainwindow.table.vTableFileRight.size();i++)
			{
				if(mainwindow.table.vTableFileRight[i].length() > 0 && 
					(mainwindow.table.vTableFileRight[i][0] == (char) 164 || 
					mainwindow.table.vTableFileRight[i][0] == (char) 165 ||
					mainwindow.table.vTableFileRight[i][0] == (char) 161))
				{
					CheckDlgButton(hDialog,IDC_RADIO2,BST_CHECKED);
					CheckDlgButton(hDialog,IDC_RADIO4,BST_CHECKED);
					CheckDlgButton(hDialog,IDC_RADIO1,BST_UNCHECKED);
					CheckDlgButton(hDialog,IDC_RADIO3,BST_UNCHECKED);
					CheckDlgButton(hDialog,IDC_RADIO5,BST_UNCHECKED);
					CheckDlgButton(hDialog,IDC_RADIO6,BST_UNCHECKED);
					break;
				}
				if(mainwindow.table.vTableFileRight[i].length() > 0 && 
					(mainwindow.table.vTableFileRight[i][0] == (char) 130 || 
					mainwindow.table.vTableFileRight[i][0] == (char) 131))
				{
					CheckDlgButton(hDialog,IDC_RADIO1,BST_CHECKED);
					CheckDlgButton(hDialog,IDC_RADIO5,BST_CHECKED);
					CheckDlgButton(hDialog,IDC_RADIO2,BST_UNCHECKED);
					CheckDlgButton(hDialog,IDC_RADIO3,BST_UNCHECKED);
					CheckDlgButton(hDialog,IDC_RADIO4,BST_UNCHECKED);
					CheckDlgButton(hDialog,IDC_RADIO6,BST_UNCHECKED);
					break;
				}
			}
			if(i >= (int)mainwindow.table.vTableFileRight.size())
			{
				CheckDlgButton(hDialog,IDC_RADIO3,BST_CHECKED);
				CheckDlgButton(hDialog,IDC_RADIO4,BST_CHECKED);
				CheckDlgButton(hDialog,IDC_RADIO1,BST_UNCHECKED);
				CheckDlgButton(hDialog,IDC_RADIO2,BST_UNCHECKED);
				CheckDlgButton(hDialog,IDC_RADIO5,BST_UNCHECKED);
				CheckDlgButton(hDialog,IDC_RADIO6,BST_UNCHECKED);
			}
			SetFocus(hDialog);
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				//extern std::vector<_tstring> vShiftEnc, vEucEnc, vRomajiEnc;
				int from = -1;
				if (IsDlgButtonChecked (hDialog, IDC_RADIO1) == BST_CHECKED)
				{
					if (IsDlgButtonChecked (hDialog, IDC_RADIO5) == BST_CHECKED)
					{
						//convert_table_from_to(vShiftEnc,vEucEnc);
					}
					else if (IsDlgButtonChecked (hDialog, IDC_RADIO6) == BST_CHECKED)
					{
						//convert_table_from_to(vShiftEnc,vRomajiEnc);
					}
					else
					{
						MessageBox(hDialog,LoadString(IDS_NOTICE_TABLE_SAME_ENCODING),LoadString(IDS_TITLE_NOTICE),MB_OK | MB_ICONINFORMATION);
					}
				}
				else if (IsDlgButtonChecked (hDialog, IDC_RADIO2) == BST_CHECKED)
				{
					if (IsDlgButtonChecked (hDialog, IDC_RADIO4) == BST_CHECKED)
					{
						//convert_table_from_to(vEucEnc,vShiftEnc);
					}
					else if (IsDlgButtonChecked (hDialog, IDC_RADIO6) == BST_CHECKED)
					{
						//convert_table_from_to(vEucEnc,vRomajiEnc);
					}
					else
					{
						MessageBox(hDialog,LoadString(IDS_NOTICE_TABLE_SAME_ENCODING),LoadString(IDS_TITLE_NOTICE),MB_OK | MB_ICONINFORMATION);
					}	
				}
				else
				{
					if (IsDlgButtonChecked (hDialog, IDC_RADIO4) == BST_CHECKED)
					{
						//convert_table_from_to(vRomajiEnc,vShiftEnc);
					}
					else if (IsDlgButtonChecked (hDialog, IDC_RADIO5) == BST_CHECKED)
					{
						//convert_table_from_to(vRomajiEnc,vEucEnc);
					}
					else
					{
						MessageBox(hDialog,LoadString(IDS_NOTICE_TABLE_SAME_ENCODING),LoadString(IDS_TITLE_NOTICE),MB_OK | MB_ICONINFORMATION);
					}
				}
				EndDialog (hDialog, 1);
				return TRUE;
			}
	
		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		case IDC_RADIO1:
			if(IsDlgButtonChecked (hDialog, IDC_RADIO4) == BST_CHECKED)
			{
				CheckDlgButton(hDialog,IDC_RADIO5,BST_CHECKED);
				CheckDlgButton(hDialog,IDC_RADIO4,BST_UNCHECKED);
			}
			return TRUE;
			break;
		case IDC_RADIO2:
			if(IsDlgButtonChecked (hDialog, IDC_RADIO5) == BST_CHECKED)
			{
				CheckDlgButton(hDialog,IDC_RADIO4,BST_CHECKED);
				CheckDlgButton(hDialog,IDC_RADIO5,BST_UNCHECKED);
			}
			return TRUE;
			break;
		case IDC_RADIO3:
			if(IsDlgButtonChecked (hDialog, IDC_RADIO6) == BST_CHECKED)
			{
				CheckDlgButton(hDialog,IDC_RADIO4,BST_CHECKED);
				CheckDlgButton(hDialog,IDC_RADIO6,BST_UNCHECKED);
			}
			return TRUE;
			break;
		case IDC_RADIO4:
			if(IsDlgButtonChecked (hDialog, IDC_RADIO1) == BST_CHECKED)
			{
				CheckDlgButton(hDialog,IDC_RADIO2,BST_CHECKED);
				CheckDlgButton(hDialog,IDC_RADIO1,BST_UNCHECKED);
			}
			return TRUE;
			break;
		case IDC_RADIO5:
			if(IsDlgButtonChecked (hDialog, IDC_RADIO2) == BST_CHECKED)
			{
				CheckDlgButton(hDialog,IDC_RADIO1,BST_CHECKED);
				CheckDlgButton(hDialog,IDC_RADIO2,BST_UNCHECKED);
			}
			return TRUE;
			break;
		case IDC_RADIO6:
			if(IsDlgButtonChecked (hDialog, IDC_RADIO3) == BST_CHECKED)
			{
				CheckDlgButton(hDialog,IDC_RADIO1,BST_CHECKED);
				CheckDlgButton(hDialog,IDC_RADIO3,BST_UNCHECKED);
			}
			return TRUE;
			break;
		}
		break;
	}
	return FALSE;
}

HRESULT ResolveIt( HWND hwnd, LPCTSTR lpszLinkFile, LPWSTR lpszPath )
{ 
	HRESULT hres; 
	IShellLink* psl; 
	TCHAR szGotPath[MAX_PATH]; 
	TCHAR szDescription[MAX_PATH]; 
	WIN32_FIND_DATA wfd; 

	*lpszPath = 0; 

	CoInitialize( NULL );
	hres = CoCreateInstance( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl );
	if (SUCCEEDED(hres))
	{ 
		IPersistFile* ppf; 

		hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); 
		if (SUCCEEDED(hres))
		{ 
			//wchar_t wsz[MAX_PATH]; 
			wchar_t *wsz=(wchar_t*)lpszLinkFile;

			//MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz, MAX_PATH); 

			hres = ppf->Load(wsz, STGM_READ); 
			if (SUCCEEDED(hres))
			{ 
				
				hres = psl->Resolve(hwnd, SLR_ANY_MATCH); 
				if (SUCCEEDED(hres))
				{ 
					
					hres = psl->GetPath(szGotPath, MAX_PATH, (WIN32_FIND_DATA *)&wfd, SLGP_SHORTPATH ); 
					if (!SUCCEEDED(hres) )
					{
						
					}

					hres = psl->GetDescription(szDescription, MAX_PATH); 
					if (!SUCCEEDED(hres)) 
					{
						
					}
					lstrcpy(lpszPath, szGotPath); 
				} 
			} 
		
		ppf->Release(); 
		} 
	
	psl->Release(); 
	}
	else
	{
	}
	CoUninitialize();
	return hres; 
}

void make_hex_only(HWND hEd)
{
	GetWindowText(hEd,pcFWText,FILL_WITH_MAX);
	int q=0;
	for(int i =0;pcFWText[i]!='\0';i++)
	{
		if(isxdigit(pcFWText[i]))
		{
			pcFWText[q]=pcFWText[i];
			q++;
		}
	}
	pcFWText[q]='\0';
	SetWindowText(hEd,pcFWText);
}

LRESULT CALLBACK HexProc(HWND hEdit, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	TCHAR a;
	LONG i=0;
	if(iMsg==WM_CHAR)
	{
		a=(TCHAR) wParam;
		if(isxdigit(a)||(a==0x8))return CallWindowProc( (WNDPROC) oldproc, hEdit, iMsg, wParam, lParam);
		else 
		{
			MessageBeep(MB_ICONEXCLAMATION);
			return 0L;
		}
	}
	else if (iMsg==WM_PASTE)
	{
		i= CallWindowProc((WNDPROC)oldproc, hEdit, iMsg, wParam, lParam);
		make_hex_only(hEdit);
	}
	else return CallWindowProc((WNDPROC)oldproc, hEdit, iMsg, wParam, lParam);
	return i;
}

void FillWithInit(HWND hDialog)
{
	if (TypeOfCurrent)
	{
		SetDlgItemTextA(hDialog,IDC_SI, "???");
		SetDlgItemTextA(hDialog,IDC_IFS, "???");
		SetDlgItemTextA(hDialog,IDC_R, "???");
	}
	else
	{
		GString bufff;
		//if(iOffsetMode == LOROM)
		//	_stprintf(bufff,_T("%d=0x%X"),buflen,toLOROM(buflen));
		//else if(iOffsetMode == HIROM)
		//	_stprintf(bufff,_T("%d=0x%X"),buflen,toHIROM(buflen));
		//else
			bufff.Format(_T("%d=0x%X"),buflen,buflen);
		SetDlgItemText(hDialog,IDC_SI, bufff);
		if(buflen)
		{
			int temp=(1+abs(tmpstart-tmpend))/buflen;
			//if(iOffsetMode == LOROM)
			//	_stprintf(bufff,_T("%d=0x%X"),temp,toLOROM(temp));
			//else if(iOffsetMode == HIROM)
			//	_stprintf(bufff,_T("%d=0x%X"),temp,toHIROM(temp));
			//else
				bufff.Format(_T("%d=0x%X"),temp,temp);
			SetDlgItemText(hDialog,IDC_IFS, bufff);
			SendDlgItemMessage(hDialog,IDC_IFS,WM_SETFONT,(WPARAM) hfdef,MAKELPARAM(TRUE, 0));
			temp=(1+abs(tmpstart-tmpend))%buflen;
			//if(iOffsetMode == LOROM)
			//	_stprintf(bufff,_T("%d=0x%X"),temp,toLOROM(temp));
			//else if(iOffsetMode == HIROM)
			//	_stprintf(bufff,_T("%d=0x%X"),temp,toHIROM(temp));
			//else
				bufff.Format(bufff,_T("%d=0x%X"),temp,temp);
			SetDlgItemText(hDialog,IDC_R, bufff);
		}
		else
		{
			SetDlgItemText(hDialog,IDC_IFS, _T("\xa5"));
			SendDlgItemMessage(hDialog,IDC_IFS,WM_SETFONT,(WPARAM) hfon,MAKELPARAM(TRUE, 0));
			SetDlgItemText(hDialog,IDC_R, _T("0=0x0"));
		}
	}
}


void MessageCopyBox(HWND hWnd, LPTSTR lpText, LPCTSTR lpCaption, UINT uType, HWND hwnd)
{
	int len=_tcslen(lpText);
	
	_tcscat(lpText,_T("\nCopy this information to the clipboard?\n"));
	if(IDYES==MessageBox (hWnd, lpText, lpCaption, MB_YESNO | uType))
	{
		
		lpText[len]=0;
		len++;
		HGLOBAL hGlobal = GlobalAlloc (GHND, len); 
		if (hGlobal != NULL)
		{
			SetCursor (LoadCursor (NULL, IDC_WAIT));
			TCHAR* pd = (TCHAR*) GlobalLock (hGlobal);
			if(pd)
			{
				_tcscpy(pd,lpText);
				GlobalUnlock (hGlobal);
				if(OpenClipboard(hwnd))
				{
					EmptyClipboard(); 
					SetClipboardData (CF_TEXT, hGlobal);
					CloseClipboard (); 
				}
				else 
					MessageBoxA (hWnd,"Clipboard failed to open correctly.","Clipboard error:",MB_OK | MB_ICONERROR);
			}
			else
			{
				GlobalFree(hGlobal);
				MessageBoxA (hWnd,"Clipboard did not lock properly.","Clipboard error:",MB_OK | MB_ICONERROR);
			}
			SetCursor (LoadCursor (NULL, IDC_ARROW));
		}
		else
			MessageBoxA (hWnd, "Not enough memory for copying to the clipboard.", "Clipboard error:", MB_OK | MB_ICONERROR);
	}
}


BOOL CALLBACK OpenPartiallyDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			TCHAR cBuf[128];
			//if(iOffsetMode == LOROM)
			//	_stprintf (cBuf, _T("x%X"), toLOROM(iStartPL));
			//else if(iOffsetMode == HIROM)
			//	_stprintf (cBuf, _T("x%X"), toHIROM(iStartPL));
			//else
				_stprintf (cBuf, _T("x%X"), iStartPL);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT1), cBuf);
			_stprintf (cBuf, _T("Size of file: %d. Load how many bytes:"), iPLFileLength);
			SetWindowText (GetDlgItem (hDialog, IDC_STATIC2), cBuf);
			_stprintf (cBuf, _T("%d"), iNumBytesPl);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT2), cBuf);
			SetFocus(hDialog);
			CheckDlgButton( hDialog, IDC_RADIO1, BST_CHECKED );
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				TCHAR cBuf[128];
				int i=0;

				if (GetDlgItemText (hDialog, IDC_EDIT1, cBuf, 128) != 0)
				{
					if (zscanf (cBuf, _T("x%X"), &iStartPL) == 0)
					{
						if (zscanf (cBuf, _T("%d"), &iStartPL) == 0)
						{
							i = -1;
						}
					}
				}
				//if(iOffsetMode == LOROM)
				//	iStartPL = fromLOROM(iStartPL);
				//else if(iOffsetMode == HIROM)
				//	iStartPL = fromHIROM(iStartPL);
				
				if( i==-1 && IsDlgButtonChecked( hDialog, IDC_RADIO1 ) == BST_CHECKED )
				{
					MessageBoxA (hDialog, "Starting offset is not recognized.", "Partial open error:", MB_OK | MB_ICONERROR);
					EndDialog (hDialog, 0);
					return 0;
				}
				
				if (GetDlgItemText (hDialog, IDC_EDIT2, cBuf, 128) != 0)
				{
					if (zscanf (cBuf, _T("%d"), &iNumBytesPl) == 0)
					{
						i = -1;
					}
				}
				
				if (i==-1)
				{
					MessageBoxA (hDialog, "Number of bytes is not recognized.", "Partial open error:", MB_OK | MB_ICONERROR);
					EndDialog (hDialog, 0);
					return 0;
				}

				if( IsDlgButtonChecked( hDialog, IDC_RADIO2 ) == BST_CHECKED )
				{
					
					iStartPL = iPLFileLength - iNumBytesPl;
					if( iStartPL < 0 )
					{
						MessageBoxA (hDialog, "Specified number of bytes to load is greater than file size.", "Partial open error:", MB_OK | MB_ICONERROR);
						EndDialog (hDialog, 0);
						return 0;
					}
				}

				EndDialog (hDialog, 1);
				return TRUE;
			}

		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK TmplDisplayDialogProc( HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam )
{
	switch( iMsg )
	{
	case WM_INITDIALOG:
		{
			SetWindowText( GetDlgItem( hDialog, IDC_EDIT1 ), pcTmplText );
			SetFocus(hDialog);
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				EndDialog (hDialog, 1);
				return TRUE;
			}
	
		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK CutDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			TCHAR cBuf[32];
			//if(iOffsetMode == LOROM)
			//	zprintf (cBuf, _T("x%X"), toLOROM(iCutOffset));
			//else if(iOffsetMode == HIROM)
			//	zprintf (cBuf, _T("x%X"), toHIROM(iCutOffset));
			//else
				zprintf (cBuf, _T("x%X"), iCutOffset);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT1), cBuf);
			//if(iOffsetMode == LOROM)
			//	zprintf (cBuf, _T("x%X"), toLOROM(iCutOffset+iCutNumberOfBytes-1));
			//else if(iOffsetMode == HIROM)
			//	zprintf (cBuf, _T("x%X"), toHIROM(iCutOffset+iCutNumberOfBytes-1));
			//else
				zprintf (cBuf, _T("x%X"), iCutOffset+iCutNumberOfBytes-1);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT2), cBuf);
			zprintf (cBuf, _T("%d"), iCutNumberOfBytes);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT3), cBuf);
			SetFocus(hDialog);
			CheckDlgButton (hDialog, IDC_CHECK1, iCutMode);
			CheckDlgButton (hDialog, IDC_RADIO1, BST_CHECKED);
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				TCHAR cBuf[64];
				if (GetDlgItemText (hDialog, IDC_EDIT1, cBuf, 64) != 0)
				{
					int i;
					if (zscanf (cBuf, _T("x%X"), &i) == 0)
					{
						if (zscanf (cBuf, _T("%d"), &i) == 0)
						{
							MessageBoxA (hDialog, "Starting offset is not recognized.", "Cut error:", MB_OK | MB_ICONERROR);
							i = -1;
						}
					}
					if (i==-1)
					{
						EndDialog (hDialog, 0);
						return 0;
					}
					//if(iOffsetMode == LOROM)
					//	iCutOffset = fromLOROM(i);
					//else if(iOffsetMode == HIROM)
					//	iCutOffset = fromHIROM(i);
					//else
						iCutOffset = i;
				}

				switch (IsDlgButtonChecked (hDialog, IDC_RADIO1))
				{
				case BST_CHECKED: 
					{
						if (GetDlgItemText (hDialog, IDC_EDIT2, cBuf, 64) != 0)
						{
							int i;
							if (zscanf (cBuf, _T("x%X"), &i) == 0)
							{
								if (zscanf (cBuf, _T("%d"), &i) == 0)
								{
									MessageBoxA (hDialog, "Ending offset is not recognized.", "Cut error:", MB_OK | MB_ICONERROR);
									i = -1;
								}
							}
							if (i==-1)
							{
								EndDialog (hDialog, 0);
								return 0;
							}
							//if(iOffsetMode == LOROM)
							//	i = fromLOROM(i);
							//else if(iOffsetMode == HIROM)
							//	i = fromHIROM(i);
							iCutNumberOfBytes = i-iCutOffset+1;
						}
					}
					break;

				default: 
					{
						if (GetDlgItemText (hDialog, IDC_EDIT3, cBuf, 64) != 0)
						{
							int i;
							if (zscanf (cBuf, _T("%d"), &i) == 0)
							{
								MessageBoxA (hDialog, "Number of bytes is not recognized.", "Cut error:", MB_OK | MB_ICONERROR);
								i = -1;
							}
							if (i==-1)
							{
								EndDialog (hDialog, 0);
								return 0;
							}
							iCutNumberOfBytes = i;
						}
					}
				}

				iCutMode = IsDlgButtonChecked (hDialog, IDC_CHECK1);
				EndDialog (hDialog, 1);
				return TRUE;
			}
	
		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK CopyDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			TCHAR cBuf[32];
			//if(iOffsetMode == LOROM)
			//	zprintf (cBuf, _T("x%X"), toLOROM(iCopyStartOffset));
			//else if(iOffsetMode == HIROM)
			//	zprintf (cBuf, _T("x%X"), toHIROM(iCopyStartOffset));
			//else
				zprintf (cBuf, _T("x%X"), iCopyStartOffset);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT1), cBuf);
			//if(iOffsetMode == LOROM)
			//	zprintf (cBuf, _T("x%X"), toLOROM(iCopyEndOffset));
			//else if(iOffsetMode == HIROM)
			//	zprintf (cBuf, _T("x%X"), toHIROM(iCopyEndOffset));
			//else
				zprintf (cBuf, _T("x%X"), iCopyEndOffset);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT2), cBuf);
			zprintf (cBuf, _T("%d"), iCopyEndOffset-iCopyStartOffset+1);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT3), cBuf);
			CheckDlgButton (hDialog, IDC_RADIO1, BST_CHECKED);
			SetFocus(hDialog);
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				TCHAR cBuf[64];
				if (GetDlgItemText (hDialog, IDC_EDIT1, cBuf, 64) != 0)
				{
					int i;
					if (zscanf (cBuf, _T("x%X"), &i) == 0)
					{
						if (zscanf (cBuf, _T("%d"), &i) == 0)
						{
							MessageBoxA (hDialog, "Starting offset is not recognized.", "Copy error:", MB_OK | MB_ICONERROR);
							i = -1;
						}
					}
					if (i==-1)
					{
						EndDialog (hDialog, 0);
						return 0;
					}
					//if(iOffsetMode == LOROM)
					//	iCopyStartOffset = fromLOROM(i);
					//else if(iOffsetMode == HIROM)
					//	iCopyStartOffset = fromHIROM(i);
					//else
						iCopyStartOffset = i;
				}

				switch (IsDlgButtonChecked (hDialog, IDC_RADIO1))
				{
				case BST_CHECKED: 
					{
						if (GetDlgItemText (hDialog, IDC_EDIT2, cBuf, 64) != 0)
						{
							int i;
							if (zscanf (cBuf, _T("x%X"), &i) == 0)
							{
								if (zscanf (cBuf, _T("%d"), &i) == 0)
								{
									MessageBoxA (hDialog, "Ending offset is not recognized.", "Copy error:", MB_OK | MB_ICONERROR);
									i = -1;
								}
							}
							if (i==-1)
							{
								EndDialog (hDialog, 0);
								return 0;
							}
							//if(iOffsetMode == LOROM)
							//	iCopyEndOffset = fromLOROM(i);
							//else if(iOffsetMode == HIROM)
							//	iCopyEndOffset = fromHIROM(i);
							//else
								iCopyEndOffset = i;
						}
						break;
					}

				default: 
					{
						if (GetDlgItemText (hDialog, IDC_EDIT3, cBuf, 64) != 0)
						{
							int i;
							if (zscanf (cBuf, _T("%d"), &i) == 0)
							{
								MessageBoxA (hDialog, "Number of bytes is not recognized.", "Copy error:", MB_OK | MB_ICONERROR);
								i = -1;
							}
							if (i==-1)
							{
								EndDialog (hDialog, 0);
								return 0;
							}
							iCopyEndOffset = iCopyStartOffset + i;
						}
						break;
					}
				}

				EndDialog (hDialog, 1);
				return TRUE;
			}

		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK CopyHexdumpDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{	
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			TCHAR cBuf[16];
			//if(iOffsetMode == LOROM)
			//	zprintf (cBuf, _T("%X"), toLOROM(iCopyHexdumpDialogStart));
			//else if(iOffsetMode == HIROM)
			//	zprintf (cBuf, _T("%X"), toHIROM(iCopyHexdumpDialogStart));
			//else
				zprintf (cBuf, _T("%X"), iCopyHexdumpDialogStart);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT1), cBuf);
			//if(iOffsetMode == LOROM)
			//	zprintf (cBuf, _T("%X"), toLOROM(iCopyHexdumpDialogEnd));
			//else if(iOffsetMode == HIROM)
			//	zprintf (cBuf, _T("%X"), toHIROM(iCopyHexdumpDialogEnd));
			//else
				zprintf (cBuf, _T("%X"), iCopyHexdumpDialogEnd);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT2), cBuf);
			if (iCopyHexdumpMode == BST_CHECKED)
				CheckDlgButton (hDialog, IDC_RADIO2, BST_CHECKED);
			else
				CheckDlgButton (hDialog, IDC_RADIO1, BST_CHECKED);
			SetFocus(hDialog);
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				TCHAR buf1[16], buf2[16];
				if (GetDlgItemText (hDialog, IDC_EDIT1, buf1, 16)!=0 && GetDlgItemText (hDialog, IDC_EDIT2, buf2, 16))
				{
					zscanf (buf1, _T("%X"), &iCopyHexdumpDialogStart);
					//if(iOffsetMode == LOROM)
					//	iCopyHexdumpDialogStart = fromLOROM(iCopyHexdumpDialogStart);
					//else if(iOffsetMode == HIROM)
					//	iCopyHexdumpDialogStart = fromHIROM(iCopyHexdumpDialogStart);
					zscanf (buf2, _T("%X"), &iCopyHexdumpDialogEnd);
					//if(iOffsetMode == LOROM)
					//	iCopyHexdumpDialogStart = fromLOROM(iCopyHexdumpDialogEnd);
					//else if(iOffsetMode == HIROM)
					//	iCopyHexdumpDialogStart = fromHIROM(iCopyHexdumpDialogEnd);
					iCopyHexdumpMode = IsDlgButtonChecked (hDialog, IDC_RADIO2);
					EndDialog (hDialog, TRUE);
				}
				else
					EndDialog (hDialog, FALSE);
				return TRUE;
			}
		case IDCANCEL:
			EndDialog (hDialog, FALSE);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK PasteDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT1), GetClipboardText());

			GString buf2;
			buf2.Format(_T("%d"), iPasteTimes);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT2), buf2);
			buf2.Format(_T("%d"), iPasteSkip);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT3), buf2);
			if( iPasteMode == 1 )
				CheckDlgButton (hDialog, IDC_RADIO1, BST_CHECKED);
			else
				CheckDlgButton (hDialog, IDC_RADIO2, BST_CHECKED);
			if( iPasteAsText == TRUE )
				CheckDlgButton (hDialog, IDC_CHECK1, BST_CHECKED);
			SetFocus(hDialog);
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				if (IsDlgButtonChecked (hDialog, IDC_CHECK1) == BST_CHECKED)
					iPasteAsText = TRUE;
				else
					iPasteAsText = FALSE;
				if (IsDlgButtonChecked (hDialog, IDC_RADIO1) == BST_CHECKED)
					iPasteMode = 1;
				else if( IsDlgButtonChecked( hDialog, IDC_RADIO2 ) == BST_CHECKED )
					iPasteMode = 2;
				TCHAR cBuf[64];
				int i;
				if (GetDlgItemText (hDialog, IDC_EDIT2, cBuf, 64) != 0)
				{
					if (zscanf (cBuf, _T("%d"), &i) == 0)
					{
						MessageBoxA (hDialog, "Number of times to paste is not recognized.", "Paste error:", MB_OK | MB_ICONERROR);
						i = -1;
					}
				}
				if (i==-1 || i==0)
				{
					MessageBoxA (hDialog, "Number of times to paste must be at least 1.", "Paste error:", MB_OK | MB_ICONERROR);
					EndDialog (hDialog, 0);
					return 0;
				}
				iPasteTimes = i;
				if (GetDlgItemText (hDialog, IDC_EDIT3, cBuf, 64) != 0)
				{
					if (zscanf (cBuf, _T("%d"), &i) == 0)
					{
						MessageBoxA (hDialog, "Number of bytes to skip is not recognized.", "Paste error:", MB_OK | MB_ICONERROR);
						EndDialog (hDialog, 0);
						return 0;
					}
				}
				iPasteSkip = i;
				iPasteMaxTxtLength = SendMessage (GetDlgItem (hDialog, IDC_EDIT1), EM_GETLIMITTEXT, 0, 0) + 1;
				pcPasteText = new TCHAR[iPasteMaxTxtLength];
				GetDlgItemText (hDialog, IDC_EDIT1, pcPasteText, iPasteMaxTxtLength);
				EndDialog (hDialog, 1);
				return TRUE;
			}
	
		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK SelectBlockDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			GString cBuf;
			//if(iOffsetMode == LOROM)
			//	cBuf.Format(_T("x%X"), toLOROM(iStartOfSelSetting));
			//else if(iOffsetMode == HIROM)
			//	cBuf.Format(_T("x%X"), toHIROM(iStartOfSelSetting));
			//else
				cBuf.Format(_T("x%X"), iStartOfSelSetting);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT1), cBuf);
			//if(iOffsetMode == LOROM)
			//	cBuf.Format(_T("x%X"), toLOROM(iEndOfSelSetting));
			//else if(iOffsetMode == HIROM)
			//	cBuf.Format(_T("x%X"), toHIROM(iEndOfSelSetting));
			//else
				cBuf.Format(_T("x%X"), iEndOfSelSetting);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT2), cBuf);
			CheckDlgButton(hDialog,IDC_RADIO1,BST_CHECKED);
			SetFocus(hDialog);
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				TCHAR cBuf[128];
				int i=0;
				if (GetDlgItemText (hDialog, IDC_EDIT1, cBuf, 128) != 0)
				{
					if (zscanf (cBuf, _T("x%X"), &iStartOfSelSetting) == 0)
					{
						if (zscanf (cBuf, _T("%d"), &iStartOfSelSetting) == 0)
						{
							MessageBoxA (hDialog, "Starting offset is not recognized.", "Select block error:", MB_OK | MB_ICONERROR);
							i = -1;
						}
					}
					//if(iOffsetMode == LOROM)
					//	iStartOfSelSetting = fromLOROM(iStartOfSelSetting);
					//else if(iOffsetMode == HIROM)
					//	iStartOfSelSetting = fromHIROM(iStartOfSelSetting);
				}
				if (i==-1)
				{
					EndDialog (hDialog, 0);
					return 0;
				}
				if (IsDlgButtonChecked(hDialog,IDC_RADIO1) && GetDlgItemText (hDialog, IDC_EDIT2, cBuf, 128) != 0)
				{
					if (zscanf (cBuf, _T("x%X"), &iEndOfSelSetting) == 0)
						if (zscanf (cBuf, _T("%d"), &iEndOfSelSetting) == 0)
						{
							MessageBoxA (hDialog, "Ending offset is not recognized.", "Select block error:", MB_OK | MB_ICONERROR);
							i = -1;
						}
					//if(iOffsetMode == LOROM)
					//	iEndOfSelSetting = fromLOROM(iEndOfSelSetting);
					//else if(iOffsetMode == HIROM)
					//	iEndOfSelSetting = fromHIROM(iEndOfSelSetting);
				}
				else
				{
					if(GetDlgItemText (hDialog, IDC_EDIT3, cBuf, 128) != 0)
					{
						if(zscanf (cBuf, _T("%d"), &iEndOfSelSetting) != 0)
						{
							iEndOfSelSetting = iStartOfSelSetting + iEndOfSelSetting - 1;
						}
						else
						{
							MessageBoxA (hDialog, "Number of bytes is not recognized.", "Select block error:", MB_OK | MB_ICONERROR);
							i = -1;
						}
					}
				}
				if (i==-1)
				{
					EndDialog (hDialog, 0);
					return 0;
				}
				EndDialog (hDialog, 1);
				return TRUE;
			}
	
		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK FillWithDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
		case WM_INITDIALOG:
			{
			HWND hEditt=GetDlgItem(hDialog, IDC_HEX);
			SendMessage(hEditt,EM_SETLIMITTEXT, (WPARAM) FILL_WITH_MAX,0);
			SetWindowText(hEditt, pcFWText);
			SetFocus(hDialog);
			SetFocus(hEditt);
			EnableWindow(hEditt,!TypeOfCurrent);
			oldproc = GetWindowLong (hEditt, GWL_WNDPROC);
			SetWindowLong(hEditt,GWL_WNDPROC,(LONG)HexProc);
			EnableWindow(GetDlgItem(hDialog, IDC_HEXSTAT),!TypeOfCurrent);

			HWND typ = GetDlgItem(hDialog, IDC_TYPE);
			SendMessage(typ,CB_ADDSTRING ,0,(LPARAM) (LPCTSTR) _T("Input"));
			SendMessage(typ,CB_ADDSTRING ,0,(LPARAM) (LPCTSTR) _T("File"));
			SendMessage(typ,CB_SETCURSEL,(WPARAM)TypeOfCurrent,0);

			HWND fn=GetDlgItem(hDialog, IDC_FN);
			SetWindowText(fn,szFWFileName);
			EnableWindow(fn,TypeOfCurrent);
			EnableWindow(GetDlgItem(hDialog, IDC_BROWSE),TypeOfCurrent);
			EnableWindow(GetDlgItem(hDialog, IDC_FILESTAT),TypeOfCurrent);

			GString bufff;
			int temp=1+abs(tmpstart-tmpend);
			//if(iOffsetMode == LOROM)
			//	bufff.Format(_T("%d=0x%X"),tmpstart,toLOROM(tmpstart));
			//else if(iOffsetMode == HIROM)
			//	bufff.Format(_T("%d=0x%X"),tmpstart,toHIROM(tmpstart));
			//else
				bufff.Format(_T("%d=0x%X"),tmpstart,tmpstart);
			SetDlgItemText(hDialog, IDC_STS,bufff);
			//if(iOffsetMode == LOROM)
			//	bufff.Format(_T("%d=0x%X"),tmpend,toLOROM(tmpend));
			//else if(iOffsetMode == HIROM)
			//	bufff.Format(_T("%d=0x%X"),tmpend,toHIROM(tmpend));
			//else
				bufff.Format(_T("%d=0x%X"),tmpend,tmpend);
			SetDlgItemText(hDialog, IDC_ES,bufff);
			//if(iOffsetMode == LOROM)
			//	bufff.Format(_T("%d=0x%X"),temp,toLOROM(temp));
			//else if(iOffsetMode == HIROM)
			//	bufff.Format(_T("%d=0x%X"),temp,toHIROM(temp));
			//else
				bufff.Format(_T("%d=0x%X"),temp,temp);
			SetDlgItemText(hDialog, IDC_SS,bufff);
			hfdef = (HFONT) SendDlgItemMessage(hDialog,IDC_R,WM_GETFONT,0,0);
			if((hfon = CreateFont(16,0,0,0,FW_NORMAL,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,_T("Symbol")))==NULL)PostQuitMessage(1);
			FillWithInit(hDialog);
			switch(asstyp)
			{
				case 0:CheckDlgButton (hDialog, IDC_EQ, BST_CHECKED);break;
				case 1:CheckDlgButton (hDialog, IDC_OR, BST_CHECKED);break;
				case 2:CheckDlgButton (hDialog, IDC_AND, BST_CHECKED);break;
				case 3:CheckDlgButton (hDialog, IDC_XOR, BST_CHECKED);break;
			}
			return 0;
		}
		break;
		case WM_COMMAND:{
			switch (LOWORD (wParam))
			{
				case IDOK:
					{
					int i;
					if(TypeOfCurrent)
					{
						GetDlgItemText(hDialog,IDC_FN,szFWFileName,_MAX_PATH);
						if((FWFile=_topen(szFWFileName,_O_RDONLY|_O_BINARY))==-1)
						{
							MessageBox(hDialog,_T("Error opening file."),_T("Fill with file error:"), MB_OK | MB_ICONERROR);
							return 1;
						}
						if((FWFilelen=_filelength(FWFile))==0)
						{
							MessageBox (hDialog, _T("Can't fill a selection with a zero-byte file."), _T("Fill with file error:"), MB_OK | MB_ICONERROR);
							_close(FWFile);
							return 1;
						}
						else if (FWFilelen==-1)
						{
							MessageBox(hDialog,_T("Error opening file."),_T("Fill with file error:"), MB_OK | MB_ICONERROR);
							_close(FWFile);
							return 1;
						}
					}
					else
					{
						if (!buflen)
						{
							MessageBox (hDialog, _T("Can't fill a selection with a string of size 0."), _T("Fill with string error:"), MB_OK | MB_ICONERROR);
							return 1;
						}
						if ((i=(GetDlgItemText (hDialog, IDC_HEX, pcFWText, FILL_WITH_MAX) == 0))||(i==FILL_WITH_MAX-1))
						{
							MessageBox (hDialog, _T("Unknown error occurred."), _T("Fill with error:"), MB_OK | MB_ICONERROR);
							return 1;
						}
						strHex2Char();
						pcFWText[(aa ? buflen : buflen*2)]='\0';

					}
					if(BST_CHECKED == IsDlgButtonChecked(hDialog,IDC_EQ))
						asstyp=0;
					else if(BST_CHECKED == IsDlgButtonChecked(hDialog,IDC_OR))asstyp=1;
					else if(BST_CHECKED == IsDlgButtonChecked(hDialog,IDC_AND))asstyp=2;
					else if(BST_CHECKED == IsDlgButtonChecked(hDialog,IDC_XOR))asstyp=3;
					DeleteObject(hfon);
					DeleteObject(hfdef);
					EndDialog (hDialog, 1);
					return 0;
				}
				break;
				case IDCANCEL:
				{
					DeleteObject(hfon);
					EndDialog (hDialog, 0);
					return 0;
				}
				break;
				case IDC_TYPE:
				{
					if(HIWORD(wParam)==CBN_SELCHANGE)
					{
						TypeOfCurrent = (char)SendMessage(GetDlgItem(hDialog, IDC_TYPE),CB_GETCURSEL,0,0);
						EnableWindow(GetDlgItem(hDialog, IDC_FN),TypeOfCurrent);
						EnableWindow(GetDlgItem(hDialog, IDC_BROWSE),TypeOfCurrent);
						EnableWindow(GetDlgItem(hDialog, IDC_FILESTAT),TypeOfCurrent);
						TypeOfCurrent=!TypeOfCurrent;
						EnableWindow(GetDlgItem(hDialog, IDC_HEX),TypeOfCurrent);
						EnableWindow(GetDlgItem(hDialog, IDC_HEXSTAT),TypeOfCurrent);
						TypeOfCurrent=!TypeOfCurrent;
						
						FillWithInit(hDialog);
					}
				}
				break;
				case IDC_BROWSE:
				{
					
					szFWFileName[0] = '\0';
					OPENFILENAME ofn;
					ofn.lStructSize = sizeof (OPENFILENAME);
					ofn.hwndOwner = hDialog;
					ofn.hInstance = NULL;
					ofn.lpstrFilter = _T("All Files (*.*)\0*.*\0\0");
					ofn.lpstrCustomFilter = NULL;
					ofn.nMaxCustFilter = 0;
					ofn.nFilterIndex = 0;
					ofn.lpstrFile = szFWFileName;
					ofn.nMaxFile = _MAX_PATH;
					ofn.lpstrFileTitle = NULL;
					ofn.lpstrInitialDir = NULL;
					ofn.lpstrTitle = NULL;
					ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
					ofn.lpstrDefExt = NULL;
					ofn.lCustData = 0L;
					ofn.lpfnHook = NULL;
					ofn.lpTemplateName = NULL;
					
					if (GetOpenFileName(&ofn))SetDlgItemText(hDialog,IDC_FN,ofn.lpstrFile);
					return 0;
				}
				break;
				case IDC_HEX:
				{
					if(HIWORD(wParam)==EN_UPDATE)
					{
						GetWindowText(GetDlgItem(hDialog, IDC_HEX), pcFWText, FILL_WITH_MAX);
						strHex2Char();
						
						FillWithInit(hDialog);
						return 0;
					}
					return 1;
				}
				break;
				default:return 1;
			}
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK BitManipDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			GString cBuf;
			//if(iOffsetMode == LOROM)
			//	cBuf.Format(_T("Manipulate bits at offset %d=0x%X"), iManipPos, toLOROM(iManipPos) );
			//else if(iOffsetMode == HIROM)
			//	cBuf.Format(_T("Manipulate bits at offset %d=0x%X"), iManipPos, toHIROM(iManipPos) );
			//else
				cBuf.Format(_T("Manipulate bits at offset %d=0x%X"), iManipPos, iManipPos );
			SetWindowText( GetDlgItem( hDialog, IDC_STATIC1 ), cBuf );
			
			cBuf.Format(_T("Value: 0x%X , %d signed, %u unsigned."), (unsigned char) cBitValue, (signed char) cBitValue, (unsigned char) cBitValue );
			SetWindowText( GetDlgItem( hDialog, IDC_STATIC2 ), cBuf );

			SetFocus(hDialog);
			if (bitval (&cBitValue,0) != 0)
				CheckDlgButton (hDialog, IDC_CHECK1, BST_CHECKED);
			if (bitval (&cBitValue,1) != 0)
				CheckDlgButton (hDialog, IDC_CHECK2, BST_CHECKED);
			if (bitval (&cBitValue,2) != 0)
				CheckDlgButton (hDialog, IDC_CHECK3, BST_CHECKED);
			if (bitval (&cBitValue,3) != 0)
				CheckDlgButton (hDialog, IDC_CHECK4, BST_CHECKED);
			if (bitval (&cBitValue,4) != 0)
				CheckDlgButton (hDialog, IDC_CHECK5, BST_CHECKED);
			if (bitval (&cBitValue,5) != 0)
				CheckDlgButton (hDialog, IDC_CHECK6, BST_CHECKED);
			if (bitval (&cBitValue,6) != 0)
				CheckDlgButton (hDialog, IDC_CHECK7, BST_CHECKED);
			if (bitval (&cBitValue,7) != 0)
				CheckDlgButton (hDialog, IDC_CHECK8, BST_CHECKED);
			return FALSE;
		}

	case WM_COMMAND:
		{
			cBitValue = 0;
			if (IsDlgButtonChecked (hDialog, IDC_CHECK8) == BST_CHECKED)
				cBitValue += 128;
			if (IsDlgButtonChecked (hDialog, IDC_CHECK7) == BST_CHECKED)
				cBitValue += 64;
			if (IsDlgButtonChecked (hDialog, IDC_CHECK6) == BST_CHECKED)
				cBitValue += 32;
			if (IsDlgButtonChecked (hDialog, IDC_CHECK5) == BST_CHECKED)
				cBitValue += 16;
			if (IsDlgButtonChecked (hDialog, IDC_CHECK4) == BST_CHECKED)
				cBitValue += 8;
			if (IsDlgButtonChecked (hDialog, IDC_CHECK3) == BST_CHECKED)
				cBitValue += 4;
			if (IsDlgButtonChecked (hDialog, IDC_CHECK2) == BST_CHECKED)
				cBitValue += 2;
			if (IsDlgButtonChecked (hDialog, IDC_CHECK1) == BST_CHECKED)
				cBitValue += 1;
			switch (LOWORD (wParam))
			{
			case IDC_CHECK1: case IDC_CHECK2: case IDC_CHECK3: case IDC_CHECK4:
			case IDC_CHECK5: case IDC_CHECK6: case IDC_CHECK7: case IDC_CHECK8:
				{
					GString cBuf;
					cBuf.Format(_T("Value: 0x%X , %d signed, %u unsigned."), (unsigned char) cBitValue, (signed char) cBitValue, (unsigned char) cBitValue );
					SetWindowText( GetDlgItem( hDialog, IDC_STATIC2 ), cBuf );
				}
				break;

			case IDOK:
				{
					EndDialog (hDialog, 1);
					return TRUE;
				}
		
			case IDCANCEL:
				EndDialog (hDialog, 0);
				return TRUE;
			}
			break;
		}
	}
	return FALSE;
}

BOOL CALLBACK ChooseDiffDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			GString cBuf;
			int i;
			cBuf.Format(_T("%d areas of difference found."), iDiffNum);
			SetWindowText (GetDlgItem (hDialog, IDC_STATIC1), cBuf);
			cBuf.Format(_T("Remaining loaded data size: %d, size of file on disk: %d."), iSrcFileLength, iDestFileLength);
			SetWindowText (GetDlgItem (hDialog, IDC_STATIC2), cBuf);
			HWND hwndList = GetDlgItem (hDialog, IDC_LIST1);
			for (i=0; i<iDiffNum; i++)
			{
				//if(iOffsetMode == LOROM)
				//	cBuf.Format(_T("%d) %d=0x%X to %d=0x%X (%d bytes)"), i+1, pdiffChoice[i].x, toLOROM(pdiffChoice[i].x),
				//	pdiffChoice[i].y, toLOROM(pdiffChoice[i].y), pdiffChoice[i].y-pdiffChoice[i].x+1);
				//else if(iOffsetMode == HIROM)
				//	cBuf.Format(_T("%d) %d=0x%X to %d=0x%X (%d bytes)"), i+1, pdiffChoice[i].x, toHIROM(pdiffChoice[i].x),
				//	pdiffChoice[i].y, toHIROM(pdiffChoice[i].y), pdiffChoice[i].y-pdiffChoice[i].x+1);
				//else
					cBuf.Format(_T("%d) %d=0x%X to %d=0x%X (%d bytes)"), i+1, pdiffChoice[i].x, pdiffChoice[i].x,
					pdiffChoice[i].y, pdiffChoice[i].y, pdiffChoice[i].y-pdiffChoice[i].x+1);
				SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR)cBuf);
			}
			SendMessage (hwndList, LB_SETCURSEL, 0, 0);
			SetFocus(hDialog);
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
			/* DA INVESTIGARE!!! */
			case IDCOPY:
				{
					int sumlen=1;
					int len=0;
					TCHAR *cBuf=(TCHAR*)malloc(sizeof(TCHAR));
					cBuf[0]=0;
					TCHAR *bt=NULL;
					HWND hwndList = GetDlgItem (hDialog, IDC_LIST1);
					int num = SendMessage(hwndList,LB_GETCOUNT,0,0);
					for(int i=0;i<num;i++)
					{	
						len=SendMessage(hwndList,LB_GETTEXTLEN,i,0)+2;
						sumlen+=len;
						bt = (TCHAR*)realloc(cBuf,sumlen*sizeof(TCHAR));
						if(bt!=NULL) cBuf=bt;
						else break;
					
						SendMessage(hwndList,LB_GETTEXT,i,(LPARAM)&cBuf[sumlen-len-1]);
						_tcscat(cBuf,_T("\r\n"));
					}
					TextToClipboard(cBuf);
					free(cBuf);
					return TRUE;
				}
				break;


		case IDOK:
			{
				HWND hwndList = GetDlgItem (hDialog, IDC_LIST1);
				iDiffNum = SendMessage (hwndList, LB_GETCURSEL, 0, 0);
				EndDialog (hDialog, 1);
				return TRUE;
			}
	
		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK EnterDecimalValueDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			GString cBuf;
			cBuf.Format(_T("%d"), iDecValDialogValue);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT1), cBuf);
			//if(iOffsetMode == LOROM)
			//	cBuf.Format(_T("x%X"), toLOROM(iDecValDialogOffset));
			//else if(iOffsetMode == HIROM)
			//	cBuf.Format(_T("x%X"), toHIROM(iDecValDialogOffset));
			//else
				cBuf.Format(_T("x%X"), iDecValDialogOffset);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT2), cBuf);
			cBuf=_T("1");
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT3), cBuf);
			CheckDlgButton (hDialog, IDC_RADIO1, BST_CHECKED);
			SetFocus(hDialog);
			return FALSE;
		}

	case WM_COMMAND:
		if (IsDlgButtonChecked (hDialog, IDC_RADIO1) == BST_CHECKED)
			iDecValDialogSize = 1;
		else if (IsDlgButtonChecked (hDialog, IDC_RADIO2) == BST_CHECKED)
			iDecValDialogSize = 2;
		else if (IsDlgButtonChecked (hDialog, IDC_RADIO3) == BST_CHECKED)
			iDecValDialogSize = 4;
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				TCHAR cBuf[16];
				if (GetDlgItemText (hDialog, IDC_EDIT1, cBuf, 16) != 0)
					zscanf (cBuf, _T("%d"), &iDecValDialogValue);
				if (GetDlgItemText (hDialog, IDC_EDIT2, cBuf, 16) != 0)
					zscanf (cBuf, _T("%X"), &iDecValDialogOffset);
				int i;
				if (zscanf (cBuf, _T("x%X"), &i) == 0)
				{
					if (zscanf (cBuf, _T("%d"), &i) == 0)
					{
						i = -1;
					}
				}
				//if(iOffsetMode == LOROM)
				//	iDecValDialogOffset = fromLOROM(i);
				//else if(iOffsetMode == HIROM)
				//	iDecValDialogOffset = fromHIROM(i);
				//else
					iDecValDialogOffset = i;
				if (GetDlgItemText (hDialog, IDC_EDIT3, cBuf, 16) != 0)
					zscanf (cBuf, _T("%d"), &iDecValDialogTimes);
				EndDialog (hDialog, 1);
				return TRUE;
			}

		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK AppendDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		{
			GString cBuf;
			cBuf.Format(_T("%d"), iAppendbytes);
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT1), cBuf);
			SetFocus(hDialog);
			return FALSE;
		}

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				TCHAR cBuf[64];
				int i=-1;
				if (GetDlgItemText (hDialog, IDC_EDIT1, cBuf, 64) != 0)
				{
					if (zscanf (cBuf, _T("%d"), &i) == 0)
					{
						MessageBox (hDialog, _T("Number of bytes to append is not recognized."), _T("Append error:"), MB_OK | MB_ICONERROR);
						i = -1;
					}
				}
				if (i==-1)
				{
					EndDialog (hDialog, 0);
					return 0;
				}
				iAppendbytes = i;
				EndDialog (hDialog, 1);
				return TRUE;
			}
	
		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK FindWithTableDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{	
	switch (iMsg)
	{
	case WM_INITDIALOG:
		
		if(sLastTableSearch != L"")
		{
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT1), sLastTableSearch.c_str());
		}
		bOptimizeBytes = true;
		if(bOptimizeBytes)
		{
			CheckDlgButton(hDialog,IDC_CHECK4,BST_CHECKED);
		}
		//if (iFindDialogBufLength == 0)
		//{
		//	
		//	iFindDialogBufLength = SendMessage (GetDlgItem (hDialog, IDC_EDIT1), EM_GETLIMITTEXT, 0, 0);
		//	
		//	if(pcFindDialogBuffer != NULL)
		//		delete [] pcFindDialogBuffer;
		//	pcFindDialogBuffer = new TCHAR[iFindDialogBufLength];
		//}
		SetFocus(hDialog);
		CheckDlgButton (hDialog, IDC_RADIO2, BST_CHECKED);
		return FALSE;

	case WM_COMMAND:
		if (IsDlgButtonChecked (hDialog, IDC_RADIO1) == BST_CHECKED)
			iFindDialogDirection = -1;
		else
			iFindDialogDirection = 1;

		switch (LOWORD (wParam))
		{
		case IDOK:
			{
			
			if(IsDlgButtonChecked(hDialog,IDC_CHECK4))
			{
				bOptimizeBytes = true;
			}
			else
			{
				bOptimizeBytes = false;
			}
			//iFindDialogBufLength = SendMessage (GetDlgItem (hDialog, IDC_EDIT1), EM_GETLIMITTEXT, 0, 0);
			//if(pcFindDialogBuffer != NULL)
			//	delete [] pcFindDialogBuffer;
			//pcFindDialogBuffer = new TCHAR[iFindDialogBufLength];
			//int result = GetDlgItemText (hDialog, IDC_EDIT1, pcFindDialogBuffer, iFindDialogBufLength);

			GString s=GetWindowText(hDialog, IDC_EDIT1);
			/* !! FIX HERE TO TRIGGER SEARCH !! */

			EndDialog (hDialog,s.GetLength());
			return TRUE;
			}
		case IDCANCEL:
			EndDialog (hDialog, 0);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK ReplaceDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		if( iPasteAsText )
			CheckDlgButton (hDialog, IDC_USETRANSLATION_CHECK, BST_UNCHECKED);
		else
			CheckDlgButton (hDialog, IDC_USETRANSLATION_CHECK, BST_CHECKED);
		if( strToReplaceData.length() != 0 )
			SetWindowText( GetDlgItem ( hDialog, IDC_TO_REPLACE_EDIT ), strToReplaceData.c_str() );
		SetFocus(hDialog);
		return FALSE;

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			EndDialog (hDialog, 0);
			return TRUE;

		case IDCANCEL:
			{
				if( IsDlgButtonChecked( hDialog, IDC_USETRANSLATION_CHECK ) == BST_CHECKED )
					iPasteAsText = FALSE;
				else
					iPasteAsText = TRUE;
				EndDialog (hDialog, 0);
				return TRUE;
			}

		case IDC_FINDPREVIOUS_BUTTON:
			{
				int (*cmp) (u8*);
				if( IsDlgButtonChecked( hDialog, IDC_MATCHCASE_CHECK ) == BST_CHECKED ) cmp = literal_u8;
				else cmp = charcase_u8;

				int buflen = SendMessage( GetDlgItem (hDialog, IDC_TO_REPLACE_EDIT), EM_GETLIMITTEXT, 0, 0 );
				TCHAR* cBuf = new TCHAR[ buflen ];
				GetDlgItemText( hDialog, IDC_TO_REPLACE_EDIT, cBuf, buflen );
				strToReplaceData = cBuf;
				if( ! mainwindow.find_and_select_data( strToReplaceData, -1, TRUE, cmp ) )
					MessageBox( hDialog, LoadString(IDS_NOTICE_STR_NOT_FOUND), LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION );
				delete [] cBuf;
			}
			break;

		case IDC_FINDNEXT_BUTTON:
			{
				int (*cmp) (u8*);
				if( IsDlgButtonChecked( hDialog, IDC_MATCHCASE_CHECK ) == BST_CHECKED ) cmp = literal_u8;
				else cmp = charcase_u8;

				int buflen = SendMessage( GetDlgItem (hDialog, IDC_TO_REPLACE_EDIT), EM_GETLIMITTEXT, 0, 0 );
				TCHAR* cBuf = new TCHAR[ buflen ];
				GetDlgItemText( hDialog, IDC_TO_REPLACE_EDIT, cBuf, buflen );
				strToReplaceData = cBuf;
				if( ! mainwindow.find_and_select_data( strToReplaceData, 1, TRUE, cmp ) )
				{
					MessageBox( hDialog, LoadString(IDS_NOTICE_STR_NOT_FOUND), LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION );
					break;
				}
				delete [] cBuf;
			}
			break;

		case IDC_FOLLOCC_BUTTON:
			{
				int (*cmp) (u8*);
				if( IsDlgButtonChecked( hDialog, IDC_MATCHCASE_CHECK ) == BST_CHECKED ) cmp = literal_u8;
				else cmp = charcase_u8;
				
				int occ_num = 0;

				int buflen = SendMessage( GetDlgItem (hDialog, IDC_TO_REPLACE_EDIT), EM_GETLIMITTEXT, 0, 0 );
				TCHAR* cBuf = new TCHAR[ buflen ];
				if( cBuf != NULL )
					GetDlgItemText( hDialog, IDC_TO_REPLACE_EDIT, cBuf, buflen );
				else
				{
					MessageBox( hDialog, _T("Could not store data to replace."), _T("Replace all error:"), MB_OK | MB_ICONERROR );
					break;
				}
				strToReplaceData = cBuf;
				delete [] cBuf;

				buflen = SendMessage( GetDlgItem( hDialog, IDC_REPLACEWITH_EDIT ), EM_GETLIMITTEXT, 0, 0 );
				cBuf = new TCHAR[ buflen ];
				if( cBuf != NULL )
					GetDlgItemText( hDialog, IDC_REPLACEWITH_EDIT, cBuf, buflen );
				else
				{
					MessageBox( hDialog, _T("Could not store data to replace with."), _T("Replace all error:"), MB_OK | MB_ICONERROR );
					break;
				}
				strReplaceWithData = cBuf;
				delete [] cBuf;

				if( IsDlgButtonChecked( hDialog, IDC_USETRANSLATION_CHECK ) == BST_CHECKED )
					iPasteAsText = FALSE;
				else
					iPasteAsText = TRUE;

				SetCursor( LoadCursor( NULL, IDC_WAIT ) );
				do
				{
					if( ! mainwindow.find_and_select_data( strToReplaceData, 1, FALSE, cmp ) )
					{
						break;
					}
					occ_num++;
					mainwindow.replace_selected_data( strReplaceWithData, FALSE );
				} while( TRUE );
				SetCursor( LoadCursor( NULL, IDC_ARROW ) );

				GString tbuf;
				tbuf.Format(LoadString(IDS_NOTICE_REPLACE_OCCURRENCES), occ_num );
				MessageBox( hDialog, tbuf, LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION );
				bReplaceFileChange = true;
				mainwindow.update_window_status();
				mainwindow.adjust_view_for_selection();
				mainwindow.update_size_change();
				mainwindow.repaint();
			}
			break;

		case IDC_PREVOCC_BUTTON:
			{
				int (*cmp) (u8*);
				if( IsDlgButtonChecked( hDialog, IDC_MATCHCASE_CHECK ) == BST_CHECKED ) cmp = literal_u8;
				else cmp = charcase_u8;

				int occ_num = 0;

				int buflen = SendMessage( GetDlgItem (hDialog, IDC_TO_REPLACE_EDIT), EM_GETLIMITTEXT, 0, 0 );
				TCHAR* cBuf = new TCHAR[ buflen ];
				if( cBuf != NULL )
					GetDlgItemText( hDialog, IDC_TO_REPLACE_EDIT, cBuf, buflen );
				else
				{
					MessageBox( hDialog, _T("Could not store data to replace."), _T("Replace all error:"), MB_OK | MB_ICONERROR );
					break;
				}
				strToReplaceData = cBuf;
				delete [] cBuf;

				buflen = SendMessage( GetDlgItem( hDialog, IDC_REPLACEWITH_EDIT ), EM_GETLIMITTEXT, 0, 0 );
				cBuf = new TCHAR[ buflen ];
				if( cBuf != NULL )
					GetDlgItemText( hDialog, IDC_REPLACEWITH_EDIT, cBuf, buflen );
				else
				{
					MessageBox( hDialog, _T("Could not store data to replace with."), _T("Replace all error:"), MB_OK | MB_ICONERROR );
					break;
				}
				strReplaceWithData = cBuf;
				delete [] cBuf;

				if( IsDlgButtonChecked( hDialog, IDC_USETRANSLATION_CHECK ) == BST_CHECKED )
					iPasteAsText = FALSE;
				else
					iPasteAsText = TRUE;

				SetCursor( LoadCursor( NULL, IDC_WAIT ) );
				do
				{
					if( ! mainwindow.find_and_select_data( strToReplaceData, -1, FALSE, cmp ) )
					{
						break;
					}
					occ_num++;
					mainwindow.replace_selected_data( strReplaceWithData, FALSE );
				} while( TRUE );
				SetCursor( LoadCursor( NULL, IDC_ARROW ) );

				GString tbuf;
				tbuf.Format(LoadString(IDS_NOTICE_REPLACE_OCCURRENCES), occ_num );
				MessageBox( hDialog, tbuf, LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION );
				bReplaceFileChange = true;
				mainwindow.update_window_status();
				mainwindow.adjust_view_for_selection();
				mainwindow.update_size_change();
				mainwindow.repaint();
			}
			break;

		case IDC_REPLACE_BUTTON:
			{
				if( IsDlgButtonChecked( hDialog, IDC_USETRANSLATION_CHECK ) == BST_CHECKED )
					iPasteAsText = FALSE;
				else
					iPasteAsText = TRUE;
				int buflen = SendMessage( GetDlgItem (hDialog, IDC_TO_REPLACE_EDIT), EM_GETLIMITTEXT, 0, 0 );
				TCHAR* cBuf = new TCHAR[ buflen ];
				if( cBuf != NULL )
				{
					if( GetDlgItemText( hDialog, IDC_TO_REPLACE_EDIT, cBuf, buflen ) != 0)
					{
						strToReplaceData = cBuf;
						delete [] cBuf;
						if( (TCHAR*) strToReplaceData.c_str() != NULL )
						{
							buflen = SendMessage( GetDlgItem( hDialog, IDC_REPLACEWITH_EDIT ), EM_GETLIMITTEXT, 0, 0 );
							cBuf = new TCHAR[ buflen ];
							GetDlgItemText( hDialog, IDC_REPLACEWITH_EDIT, cBuf, buflen );
							strReplaceWithData = cBuf;
							delete [] cBuf;
							mainwindow.replace_selected_data( strReplaceWithData );
							bReplaceFileChange = true;
						}
						else
						{
							MessageBox( hDialog, _T("Could not store data to replace."), _T("Replace error:"), MB_OK | MB_ICONERROR );
						}
						break;
					}
					else
					{
						delete [] cBuf;
						MessageBox( hDialog, _T("Could not read out data to replace."), _T("Replace error:"), MB_OK | MB_ICONERROR );
					}
				}
				else
				{
					MessageBox( hDialog, _T("Could not allocate memory for data to replace."), _T("Replace error:"), MB_OK | MB_ICONERROR );
				}
			}
			break;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK ReplaceWithTableDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (iMsg)
	{
	case WM_INITDIALOG:
		bOptimizeBytes = true;
		if(bOptimizeBytes)
		{
			CheckDlgButton(hDialog,IDC_CHECK4,BST_CHECKED);
		}
		if(sLastTableReplace != _T("") )
		{
			SetWindowText( GetDlgItem ( hDialog, IDC_TO_REPLACE_EDIT ), sLastTableReplace.c_str() );
		}
		SetFocus(hDialog);
		return FALSE;

	case WM_COMMAND:
		switch (LOWORD (wParam))
		{
		case IDOK:
			{
				
			EndDialog (hDialog, 0);
			return TRUE;
			}
		case IDCANCEL:
			{
				int buflen = SendMessage( GetDlgItem (hDialog, IDC_TO_REPLACE_EDIT), EM_GETLIMITTEXT, 0, 0 );
				TCHAR* cBuf = new TCHAR[ buflen ];
				GetDlgItemText( hDialog, IDC_TO_REPLACE_EDIT, cBuf, buflen );
				sLastTableReplace = (_tstring) cBuf;
				delete [] cBuf;
				EndDialog (hDialog, 0);
				return TRUE;
			}

		case IDC_FINDPREVIOUS_BUTTON:
			{
				if(IsDlgButtonChecked(hDialog,IDC_CHECK4))
				{
					bOptimizeBytes = true;
				}
				else
				{
					bOptimizeBytes = false;
				}
				int (*cmp) (u8*);
				cmp = literal_u8;
				int buflen = SendMessage( GetDlgItem (hDialog, IDC_TO_REPLACE_EDIT), EM_GETLIMITTEXT, 0, 0 );
				TCHAR* cBuf = new TCHAR[ buflen ];
				GetDlgItemText( hDialog, IDC_TO_REPLACE_EDIT, cBuf, buflen );
				strToReplaceData = cBuf;
				
				_tstring temp = mainwindow.create_tablefind_string((_tstring) strToReplaceData);
				strToReplaceData = temp;
				if( ! mainwindow.find_and_select_data( strToReplaceData, -1, TRUE, cmp ) )
					MessageBox( hDialog, LoadString(IDS_NOTICE_STR_NOT_FOUND), LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION );
				delete [] cBuf;
			}
			break;

		case IDC_FINDNEXT_BUTTON:
			{
				if(IsDlgButtonChecked(hDialog,IDC_CHECK4))
				{
					bOptimizeBytes = true;
				}
				else
				{
					bOptimizeBytes = false;
				}
				int (*cmp) (u8*);
				cmp = literal_u8;
				int buflen = SendMessage( GetDlgItem (hDialog, IDC_TO_REPLACE_EDIT), EM_GETLIMITTEXT, 0, 0 );
				TCHAR* cBuf = new TCHAR[ buflen ];
				GetDlgItemText( hDialog, IDC_TO_REPLACE_EDIT, cBuf, buflen );
				strToReplaceData = cBuf;
				
				_tstring temp = mainwindow.create_tablefind_string((_tstring) strToReplaceData);
				strToReplaceData = temp.c_str();
				if( ! mainwindow.find_and_select_data( strToReplaceData, 1, TRUE, cmp ) )
				{
					MessageBox( hDialog, LoadString(IDS_NOTICE_STR_NOT_FOUND), LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION );
					break;
				}
				delete [] cBuf;
			}
			break;

		case IDC_FOLLOCC_BUTTON:
			{
				int (*cmp) (u8*);
				cmp = literal_u8;
				
				int occ_num = 0;

				int buflen = SendMessage( GetDlgItem (hDialog, IDC_TO_REPLACE_EDIT), EM_GETLIMITTEXT, 0, 0 );
				TCHAR* cBuf = new TCHAR[ buflen ];
				if( cBuf != NULL )
					GetDlgItemText( hDialog, IDC_TO_REPLACE_EDIT, cBuf, buflen );
				else
				{
					MessageBox( hDialog, _T("Could not store data to replace."), _T("Table replace all error:"), MB_OK | MB_ICONERROR );
					break;
				}
				strToReplaceData = cBuf;
				_tstring temp = mainwindow.create_tablefind_string((_tstring) strToReplaceData);
				strToReplaceData = temp.c_str();
				delete [] cBuf;

				buflen = SendMessage( GetDlgItem( hDialog, IDC_REPLACEWITH_EDIT ), EM_GETLIMITTEXT, 0, 0 );
				cBuf = new TCHAR[ buflen ];
				if( cBuf != NULL )
					GetDlgItemText( hDialog, IDC_REPLACEWITH_EDIT, cBuf, buflen );
				else
				{
					MessageBox( hDialog, _T("Could not store data to replace with."), _T("Table replace all error:"), MB_OK | MB_ICONERROR );
					break;
				}
				strReplaceWithData = cBuf;
				temp = mainwindow.create_tablefind_string((_tstring) strReplaceWithData);
				strReplaceWithData = temp.c_str();
				delete [] cBuf;

				iPasteAsText = FALSE;
				SetCursor( LoadCursor( NULL, IDC_WAIT ) );
				do
				{
					if( ! mainwindow.find_and_select_data( strToReplaceData, 1, FALSE, cmp ) )
					{
						break;
					}
					occ_num++;
					mainwindow.replace_selected_data( strReplaceWithData, FALSE );
				} while( TRUE );
				SetCursor( LoadCursor( NULL, IDC_ARROW ) );

				GString tbuf;
				tbuf.Format(LoadString(IDS_NOTICE_REPLACE_OCCURRENCES), occ_num );
				MessageBox( hDialog, tbuf, LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION );
				bReplaceFileChange = TRUE;
				mainwindow.update_window_status();
				mainwindow.adjust_view_for_selection();
				mainwindow.update_size_change();
				mainwindow.repaint();
			}
			break;

		case IDC_PREVOCC_BUTTON:
			{
				
				int (*cmp) (u8*);
				cmp = literal_u8;
				int occ_num = 0;

				int buflen = SendMessage( GetDlgItem (hDialog, IDC_TO_REPLACE_EDIT), EM_GETLIMITTEXT, 0, 0 );
				TCHAR* cBuf = new TCHAR[ buflen ];
				if( cBuf != NULL )
					GetDlgItemText( hDialog, IDC_TO_REPLACE_EDIT, cBuf, buflen );
				else
				{
					MessageBox( hDialog, _T("Could not store data to replace."), _T("Table replace all error:"), MB_OK | MB_ICONERROR );
					break;
				}
				strToReplaceData = cBuf;
				_tstring temp = mainwindow.create_tablefind_string((_tstring) strToReplaceData);
				strToReplaceData = temp;
				delete [] cBuf;

				buflen = SendMessage( GetDlgItem( hDialog, IDC_REPLACEWITH_EDIT ), EM_GETLIMITTEXT, 0, 0 );
				cBuf = new TCHAR[ buflen ];
				if( cBuf != NULL )
					GetDlgItemText( hDialog, IDC_REPLACEWITH_EDIT, cBuf, buflen );
				else
				{
					MessageBox( hDialog, _T("Could not store data to replace with."), _T("Table replace all error:"), MB_OK | MB_ICONERROR );
					break;
				}
				strReplaceWithData = cBuf;
				temp = mainwindow.create_tablefind_string((_tstring) strReplaceWithData);
				strReplaceWithData = temp;
				delete [] cBuf;

				iPasteAsText = FALSE;

				SetCursor( LoadCursor( NULL, IDC_WAIT ) );
				do
				{
					if( ! mainwindow.find_and_select_data( strToReplaceData, -1, FALSE, cmp ) )
					{
						break;
					}
					occ_num++;
					mainwindow.replace_selected_data( strReplaceWithData, FALSE );
				} while( TRUE );
				SetCursor( LoadCursor( NULL, IDC_ARROW ) );

				GString tbuf;
				tbuf.Format(LoadString(IDS_NOTICE_REPLACE_OCCURRENCES), occ_num );
				MessageBox( hDialog, tbuf, LoadString(IDS_TITLE_NOTICE), MB_OK | MB_ICONINFORMATION );
				bReplaceFileChange = TRUE;
				mainwindow.update_window_status();
				mainwindow.adjust_view_for_selection();
				mainwindow.update_size_change();
				mainwindow.repaint();
			}
			break;

		case IDC_REPLACE_BUTTON:
			{
				iPasteAsText = FALSE;
				int buflen = SendMessage( GetDlgItem (hDialog, IDC_TO_REPLACE_EDIT), EM_GETLIMITTEXT, 0, 0 );
				TCHAR* cBuf = new TCHAR[ buflen ];
				if( cBuf != NULL )
				{
					if( GetDlgItemText( hDialog, IDC_TO_REPLACE_EDIT, cBuf, buflen ) != 0)
					{
						strToReplaceData = cBuf;
						_tstring temp = mainwindow.create_tablefind_string((_tstring) strToReplaceData);
						strToReplaceData = temp;
						delete [] cBuf;
						if( (char*) strToReplaceData.c_str() != NULL )
						{
							buflen = SendMessage( GetDlgItem( hDialog, IDC_REPLACEWITH_EDIT ), EM_GETLIMITTEXT, 0, 0 );
							cBuf = new TCHAR[ buflen ];
							GetDlgItemText( hDialog, IDC_REPLACEWITH_EDIT, cBuf, buflen );
							strReplaceWithData = cBuf;
							_tstring temp = mainwindow.create_tablefind_string((_tstring) strReplaceWithData);
							strReplaceWithData = temp;
							delete [] cBuf;
							mainwindow.replace_selected_data( strReplaceWithData );
							bReplaceFileChange = TRUE;
						}
						else
						{
							MessageBox( hDialog, _T("Could not store data to replace."), _T("Table replace error:"), MB_OK | MB_ICONERROR );
						}
						break;
					}
					else
					{
						delete [] cBuf;
						MessageBox( hDialog, _T("Could not read out data to replace."), _T("Table replace error:"), MB_OK | MB_ICONERROR );
					}
				}
				else
				{
					MessageBox( hDialog, _T("Could not allocate memory for data to replace."), _T("Table replace error:"), MB_OK | MB_ICONERROR );
				}
			}
			break;
		}
		break;
	}
	return FALSE;
}

BOOL CALLBACK SearchRelativeDialogProc (HWND hDialog, UINT iMsg, WPARAM wParam, LPARAM lParam)
{	
	switch (iMsg)
	{
	case WM_INITDIALOG:
		
		//if (iFindDialogBufLength == 0)
		//{
		//	
		//	iFindDialogBufLength = SendMessage (GetDlgItem (hDialog, IDC_EDIT1), EM_GETLIMITTEXT, 0, 0);
		//	
		//	if(pcFindDialogBuffer != NULL)
		//		delete [] pcFindDialogBuffer;
		//	pcFindDialogBuffer = new TCHAR[iFindDialogBufLength];
		//}
		SetFocus(hDialog);
		
		if (iFindDialogLastLength != 0 && bLastSearchType == 1)
			SetWindowText (GetDlgItem (hDialog, IDC_EDIT1), mainwindow.src_helper.GetSearchString() /*pcFindDialogBuffer*/);
		CheckDlgButton (hDialog, IDC_RADIO2, BST_CHECKED);
		return FALSE;

	case WM_COMMAND:
		if (IsDlgButtonChecked (hDialog, IDC_RADIO1) == BST_CHECKED)
			iFindDialogDirection = -1;
		else
			iFindDialogDirection = 1;

		switch (LOWORD (wParam))
		{
		case IDOK:
			{
			if(IsDlgButtonChecked(hDialog,IDC_CHECK1))
				bFileRelativeResults = true;
			//iFindDialogBufLength = SendMessage (GetDlgItem (hDialog, IDC_EDIT1), EM_GETLIMITTEXT, 0, 0);
			//if(pcFindDialogBuffer != NULL)
			//	delete [] pcFindDialogBuffer;
			//pcFindDialogBuffer = new TCHAR[iFindDialogBufLength];
			//int result = GetDlgItemText (hDialog, IDC_EDIT1, pcFindDialogBuffer, iFindDialogBufLength);
			GString s=GetWindowText(hDialog,IDC_EDIT1);
			EndDialog (hDialog,s.GetLength());
			return TRUE;
			}
			break;
		case IDCANCEL:
			EndDialog (hDialog, -5);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

////////////////////////////////////////////////
_tstring FixFilename(LPCTSTR cFileName, LPCTSTR extension)
{
	_tstring sFileName;
	LPCTSTR ext=&cFileName[_tcslen(cFileName)-_tcslen(extension)];

	if(_tcsicmp(ext,extension) == 0)
		sFileName = (_tstring) cFileName;
	else
		sFileName = (_tstring) ((_tstring)cFileName+extension);

	return sFileName;
}

int intvalue(_tstring &s)
/* PURPOSE:    Converts a string to a long
   RETURNS:    The equivalent long (0 if none found).
*/
{
	return s.c_str() ? _tstoi(s.c_str()) : 0;
}

/*...........................................................................*/

double floatvalue(_tstring &s)
/* PURPOSE:    Converts a string to floating point
   RETURNS:    The equivalent floating point
*/
{
	return s.c_str() ? _tstoi(s.c_str()) : 0;
}

/*...........................................................................*/

_tstring stringvalue(int n)
/* PURPOSE:    Converts a long to a string.
   RECEIVES:   The long to convert.
*/
{
	TCHAR cBuf[3*sizeof(int)];
	_stprintf(cBuf, _T("%d"), n);
	return _tstring(cBuf);
}

/*...........................................................................*/

_tstring stringvalue(double d)
/* PURPOSE:    Converts a double to a string (in general format, with 6 digits)
   RECEIVES:   The double to convert.
*/
{
	TCHAR cBuf[2 * DBL_DIG];
	_stprintf(cBuf, _T("%g"), d);
	return _tstring(cBuf);
}
